I am getting the following error when calling my Web Api
System.Net.Http.HttpClient.IMyDataService.LogicalHandler[101] End processing HTTP request after 70.5ms – 400
I am using PostAsJsonAsync and it’s like it doesn’t like the TValue object in the client.
When running from the client in debug mode, the break point in the Api controller in never hit and status 400 is instantly returned.
I have tested this by just sending a simple string and it works, it just doesn’t work when using a model.
public async Task<MyModel> AddNew(MyModel model)
{
var jsonSerializerOptions = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var response = await _httpClient.PostAsJsonAsync($"api/add-model", model, jsonSerializerOptions);
// omitted for brevity
return model;
}
This is confusing me because I have another API controller with a post request; in another component and it works perfectly fine.
The controller:
[HttpPost("add-model")]
public async Task<ActionResult<MyModel>> AddNewModel(MyModel model)
{
// omitted for brevity
}
2
Answers
The issue was because I was only populating some of the fields to send over to the API. I tried a test to populate every field and send it over and it has worked and returned a status 200.
Yes, Pass all fields , else make properties as nullable which you are not passing from class object.