Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"400/The input was not valid" when calling ASP.NET Core Web API

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 &#x27;----------------------------179455694862693501539465&#x27; is not a valid number.

What are these numbers and where are they coming from?

like image 729
birdus Avatar asked Dec 16 '25 19:12

birdus


1 Answers

ModelState is only validated if it's able to successfully deserialize the post body/bind.

You were not specific in your question, but it sounds as if you're posting as x-www-form-urlencoded. If that's the case, you should not have [FromBody]. However, bear in mind that it's an either/or situation, so if you want to eventually post via JSON, then you should keep the attribute and post JSON from Postman instead.

Aside from that, ensure that you're either not using antiforgery token validation or that you are posting a valid token along with the request. Failing antiforgery validation will short circuit the request and return a 400 immediately.

If you are posting JSON, ensure that your JSON is valid. If the JSON is invalid, you'll get a 400 immediately.

like image 197
Chris Pratt Avatar answered Dec 19 '25 08:12

Chris Pratt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!