Hail!
I don’t know if the question is relevant given the time that was asked. Anyway, here’s my contribution:
The ideal answer would be 400: Bad Request for all the scenarios you presented. I understand that in these cases presented the client sent an incorrect request to be interpreted by the server.
I will try to detail each scenario.
- PUT in Object without Id
PUT to localhost:8080/users/1 URL with a JSON without the Id attribute.
Enter in the detail of the error the interface expected by the request. How you are using the plural of semantics (users), can return the list of registered users. The URL /usuario
(singular) without the Id must respond with status 400 and inform in detail of the error the problem.
- PUT in Object with Id other than parameter
PUT to localhost:8080/users/1 URL with a JSON with Id 2, that is, different from what was passed in the parameter.
In this case, you can take two paths:
- Ignore the sent in JSON and take the truth of the URL or;
- Return an error code 400 as I put above stating the inconsistency.
- GET with invalid parameter
GET in the localhost:8080 URL/users/search? sex=ABC, that is, an invalid sex for the system (the correct sex would be sex=male or female).
Same 400 status. Request inconsistency informing in the detail of the error the value of the expected parameters.
In this case you could synthesize your URL to localhost:8080/usuarios/?sexo=ABC
. Speechless quest. This is because the parameters in the URL already connote the query.
- JSON POST already with id.
Perform a POST in order to persist some new data, but already with the id.
You can handle it the same way as in #2. Ignore the sent Id or return the error explaining the inconsistency.
I also suggest defining a domain for the errors returned, for example:
{
codigo: 0001,
mensagem: 'o parâmetro sexo pode receber como valor apenas feminino ou masculino. Corrija a requisição e tente novamente',
status: '400' ...
}
This does not exclude you returning the correct HTTP status.
For more information, see this page: http://www.restapitutorial.com/httpstatuscodes.html
Victor, basically you want to know the type of http return most indicated for each of these cases right?
– Leonardo Villela
Exactly. To do the correct treatment of Status in my application
– Victor Magalhães