C
C#8mo ago
Keyvan

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.
[HttpPut("Edit")]
public async Task<ActionResult> UserEdit([FromBody] UserModel user, string password)
{
var retrievedEntity = _context.Users.Find(user.UserName);
if (retrievedEntity == null) {
return NotFound("User was not found in database");
}

bool changeApproved = Security.VerifyPassword(password, user.Password);
if (!changeApproved) {
return BadRequest("Wrong password");
}

var userEntity = UserServices.ModelToEntity(user);

_context.Users.Update(userEntity);
await _context.SaveChangesAsync();
return Ok("User successfully updated");
}
[HttpPut("Edit")]
public async Task<ActionResult> UserEdit([FromBody] UserModel user, string password)
{
var retrievedEntity = _context.Users.Find(user.UserName);
if (retrievedEntity == null) {
return NotFound("User was not found in database");
}

bool changeApproved = Security.VerifyPassword(password, user.Password);
if (!changeApproved) {
return BadRequest("Wrong password");
}

var userEntity = UserServices.ModelToEntity(user);

_context.Users.Update(userEntity);
await _context.SaveChangesAsync();
return Ok("User successfully updated");
}
No description
16 Replies
becquerel
becquerel8mo ago
what body are you calling the API with? these errors come from ASP.NET itself, before your code is getting called
Keyvan
KeyvanOP8mo ago
@Becquerel (ping on reply please)What do you mean by what body
becquerel
becquerel8mo ago
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
Keyvan
KeyvanOP8mo ago
i am sending a json file as the body of the request
becquerel
becquerel8mo ago
and do you think that JSON is a valid request... your screenshot explains why ASP.NET thinks it isn't valid
Keyvan
KeyvanOP8mo ago
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 required
becquerel
becquerel8mo ago
because it thinks the user you are writing in your JSON is invalid
becquerel
becquerel8mo ago
because of this
No description
becquerel
becquerel8mo ago
i would double-check how you are writing this lastOnlineTime in your JSON
becquerel
becquerel8mo ago
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.
Keyvan
KeyvanOP8mo ago
it says valid json
becquerel
becquerel8mo ago
it may be valid JSON but an invalid format for C#
Keyvan
KeyvanOP8mo ago
yeah so the datetime format given to the UserModels field "LastOnlineTime" is converted wrong?
becquerel
becquerel8mo ago
that's what the error says, yeah consider testing it with the DateTime.Parse method
Keyvan
KeyvanOP8mo ago
but how can i parse it before it reaches the deserializer?
becquerel
becquerel8mo ago
you can make a console app which just runs DateTime.Parse in your Main() method
Want results from more Discord servers?
Add your server