C
C#7mo 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
Becquerel7mo ago
what body are you calling the API with? these errors come from ASP.NET itself, before your code is getting called
Keyvan
KeyvanOP7mo ago
@Becquerel (ping on reply please)What do you mean by what body
Becquerel
Becquerel7mo 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
KeyvanOP7mo ago
i am sending a json file as the body of the request
Becquerel
Becquerel7mo ago
and do you think that JSON is a valid request... your screenshot explains why ASP.NET thinks it isn't valid
Keyvan
KeyvanOP7mo 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
Becquerel7mo ago
because it thinks the user you are writing in your JSON is invalid
Becquerel
Becquerel7mo ago
because of this
No description
Becquerel
Becquerel7mo ago
i would double-check how you are writing this lastOnlineTime in your JSON
Becquerel
Becquerel7mo 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
KeyvanOP7mo ago
it says valid json
Becquerel
Becquerel7mo ago
it may be valid JSON but an invalid format for C#
Keyvan
KeyvanOP7mo ago
yeah so the datetime format given to the UserModels field "LastOnlineTime" is converted wrong?
Becquerel
Becquerel7mo ago
that's what the error says, yeah consider testing it with the DateTime.Parse method
Keyvan
KeyvanOP7mo ago
but how can i parse it before it reaches the deserializer?
Becquerel
Becquerel7mo 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