0
Sirs,
I have a webapi whose end point is used to return an employee’s information.
Consider the two scenarios below :
- In scenario 1 the URL below is invalid, non-existent. Of course the server returns 404 (Not Found)
https://sistema.com.br/api/v1/employees/1
Obs. employees is incorrect.
In the second scenario, the url is valid, the code falls into the Controller Action, connects to the base by looking for the employee with ID = 1. However, this does not exist. Hence I need to explicitly inform the Status Code for Response.
;
try { var funcionario = await Task.FromResult(new FuncionarioApplication().ObtemFuncionario(id)); if (funcionario!=null) { ApiResult.StatusCode = 200; } else { ApiResult.StatusCode = 204; } } catch (Exception e) { ApiResult.AddError($"Ocorreu uma Exception ao tentar localizar o funcionario. Detalhe : {e.Message} Origem : {e.StackTrace}"); ApiResult.StatusCode = 400; } return base.StatusCode(Result.StatusCode, ApiResult.GetResult());
Here I am returning code 204 - No Content
400 Invalid or 416 Requested Request for Unsatisfactory Track
– Augusto Vasques
if the product does not exist is 404, "does not exist", was not an invalid request
– Ricardo Pontual
I supplemented the question with more details.
– Adelson Silva
404 is perfectly acceptable in its
else
, since it is just that, something not found. 204 is "found" (or processed), but it will not present anything in the answer. An example of this is a post/put action to save something that wouldn’t need a complete "commit", something like saving automatic drafts of messages that are being edited.Something else,400 Bad request
does not make sense, it would be a problem if it were failure in REQUEST and not on the server side, if an Exception occurred is because there is something very wrong, status500 Internal Server Error
would be more appropriate.– Guilherme Nascimento
Show.. vlw brother
– Adelson Silva