I'm testing my Web API using postman and am getting back a "400 Bad Request" or "The input was not valid" in Postman. The break point on the first line of my action isn't getting hit. How can I figure out what's going on behind the scenes?
I would show a screenshot of Postman, but I can't get the image to upload. It's simply a POST with several fields specified for the Body. Here's the URL:
http://localhost:52126/api/v1/itinerary
Here's the action:
[HttpPost]
[Route("api/v1/itinerary")]
public async Task<ActionResult> AddItineraryAsync([FromBody] Itinerary itinerary)
{
if (!ModelState.IsValid) // Break point on this line not getting hit!
{
return BadRequest(ModelState);
}
await Logic.AddItineraryAsync(itinerary);
return Ok();
}
Originally, I didn't have [FromBody] specified for the action. It doesn't work either way. What can I check to figure out what's going on?
Clarification (in response to Chris Pratt):
I'm in the early stages of writing my first mobile app, and, being a bottom-up kind of guy, I'm getting some Web API calls working first. I assume I'll be passing JSON from the mobile app to this server layer.
I haven't done anything having to do with anti-forgery tokens in this Web API project, so I assume those aren't in play...unless that's something that's coming from Postman.
Removing the parameter from the action does allow the execution to fall through to my break point, so the error does seem to be with ASP.NET's attempt at binding.
After digging around in Postman a bit, I did see that the header was specifying x-www-form-urlencoded for the Content-Type. I changed it to application/json, as you suggested. However, that didn't seem to make a difference.
More Info:
After trying what Carl suggested below, I have more information. Execution dropped down into the action and I got a look at what is being passed from Postman. Here it is:
----------------------------179455694862693501539465
Content-Disposition: form-data; name="StartPoint"
Tacoma
----------------------------179455694862693501539465
Content-Disposition: form-data; name="EndPoint"
Portland
----------------------------179455694862693501539465
Content-Disposition: form-data; name="TravelDate"
2018-8-21 07:00:00 AM
----------------------------179455694862693501539465
Content-Disposition: form-data; name="Name"
Test Itinerary
----------------------------179455694862693501539465
Content-Disposition: form-data; name="UserAccountId"
2
----------------------------179455694862693501539465
Content-Disposition: form-data; name="Comment"
Just doin' some testing.
----------------------------179455694862693501539465--
The Json deserialization is failing with:
JsonReaderException: Input string '----------------------------179455694862693501539465' is not a valid number.
What are these numbers and where are they coming from?
179455694862693501539465, it seems you are using PostMan withapplication/x-www-form-urlencodedasContent-Type. I suggest you follow steps: 1. keep your api withAddItineraryAsync([FromBody] Itinerary itinerary); 2.clear your headers in postman 3.Bodytab chooserawwithJSON(application/json)4. enter valid json string or just with{}5. make sure there is no additional headers exceptapplication/jsonasContent-Typeinheaderstab. 6. send request to check whether api method will be hit.