Testing my UserEdit Api endpoint using swagger fails and throws a code 400 response
I am trying to use Swagger to test my api endpoints but i get a code 400 when i test them.
16 Replies
what body are you calling the API with? these errors come from ASP.NET itself, before your code is getting called
@Becquerel (ping on reply please)What do you mean by what body
what query are you sending into the API? the JSON or the XML?
the error message you're getting is saying that your query is invalid, it doesn't meet the criteria your UserModel stipulates
i am sending a json file
as the body of the request
and do you think that JSON is a valid request...
your screenshot explains why ASP.NET thinks it isn't valid
i am new to this so i dont see any problem
public async Task<ActionResult> UserEdit([FromBody] UserModel user, string password)
I am sending the user, then why is it saying user field is requiredbecause it thinks the user you are writing in your JSON is invalid
because of this
i would double-check how you are writing this lastOnlineTime in your JSON
consider trying a tool like https://jsonlint.com/
JSON Online Validator and Formatter - JSON Lint
JSONLint is the free online validator, json formatter, and json beautifier tool for JSON, a lightweight data-interchange format. You can format json, validate json, with a quick and easy copy+paste.
it says valid json
it may be valid JSON but an invalid format for C#
yeah so the datetime format given to the UserModels field "LastOnlineTime" is converted wrong?
that's what the error says, yeah
consider testing it with the DateTime.Parse method
but how can i parse it before it reaches the deserializer?
you can make a console app which just runs DateTime.Parse in your Main() method