2

I have an angular app thats sends an array of objects and a .NET API server.

The problem is that my .NET does receive the data but it cannot bind it to objects correctly.

Angular Frontend App:

data: IAccountBookKeeping[] = [];

onClickSendToAPI(){

const httpOptions = {
  headers: new HttpHeaders({'Content-Type': 'application/json'})
}

 const stringify = fastJson({
  type: 'object',
  properties: {
    AccountingDate: {
      type: 'string'
    },
    RegistrationNo: {
      type: 'string'
    },
    Currency: {
      type: 'string'
    },
    IDKT: {
      type: 'string'
    },
    OriginalIDKT: {
      type: 'string'
    },
    CounterAccountIDKT: {
      type: 'string'
    },
    ProjectCode: {
      type: 'string'
    },
    Balance: {
      type: 'string'
    },
    Text: {
      type: 'string'
    }
  }
})


const jsonString: string[] = [];

this.data.forEach(element => {
  jsonString.push(stringify(this.data[0]));
});

this.http.post('http://localhost:5000/api/GFSAccount',
JSON.stringify(jsonString),
 httpOptions).subscribe(reponse =>
console.log(reponse));

}

What Angular Frontend send to the API

[" {\"AccountingDate\":\"20171130\",\"RegistrationNo\":\"39A1\",\"Currency\":\"DKK\",\"IDKT\":\"34HS991016\",\"OriginalIDKT\":\"test\",\"CounterAccountIDKT\":\"34HS980876\",\"ProjectCode\":\"078\",\"Balance\":\"1\",\"Text\":\"006-Hest prov-Ytd NOV17\"}","{\"AccountingDate\":\"20171130\",\"RegistrationNo\":\"39A1\",\"Currency\":\"DKK\",\"IDKT\":\"34HS991016\",\"OriginalIDKT\":\"test\",\"CounterAccountIDKT\":\"34HS980876\",\"ProjectCode\":\"078\",\"Balance\":\"1\",\"Text\":\"006-Hest prov-Ytd NOV17\"}"

ASP .NET API Server Receiving

    [HttpPost]
    public IActionResult Post([FromBody] IEnumerable<AccountBookKeeping> request)
    {
      return Ok(request);
    }

ASP .NET AccountBookKeeping Class

public class AccountBookKeeping
{
    public string AccountingDate { get; set; }
    public string RegistrationNo { get; set; }
    public string IDKT { get; set; }
    public string OriginalIDKT { get; set; }
    public string CounterAccountIDKT { get; set; }
    public string Text { get; set; }
    public string ProjectCode { get; set; }
    public string Currency { get; set; }
    public string Balance { get; set; }
}
2
  • What is this.data? The array you want to post? Commented Mar 11, 2020 at 10:10
  • Yes, it's the array I would like to send: data: IAccountBookKeeping[] = []; Commented Mar 11, 2020 at 10:11

1 Answer 1

1

If you want to post an array as the body of a post, then just pass the array in directly. There's no need to heavily process it through JSON.stringify.

const url = 'http://localhost:5000/api/GFSAccount';
this.http.post(url, this.data, httpOptions).subscribe(reponse =>
  console.log(reponse);
});

As an aside, your existing loop is just building an array with the first element repeated:

this.data.forEach(element => {
  jsonString.push(stringify(this.data[0]));
});

But this step is irrelevant if you just pass the array in as the body.

Sign up to request clarification or add additional context in comments.

10 Comments

I tried this. ASP. NET will give back a Null. I will also get a status code 307 temporary redirect.
What about public IActionResult Post(AccountBookKeeping[] request)?
This will give 201 Ok but send back an empty array
Why a 201? Your code is returning Ok which should be a 200. When I run this.http.post(`${apiUrl}/test`, [1, 2, 3]) my C# (Framework) controller receives the data: public IHttpActionResult Test(int[] data)
I've meant 200.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.