HTTP - Correct return types

Asked

Viewed 177 times

2

I have some questions regarding the most appropriate status for each situation to follow:

  1. PUT in Object without Id

PUT in the URL localhost:8080/users/1 with a JSON without the attribute Id.

  1. PUT in Object with Id other than parameter

PUT in the URL localhost:8080/users/1 with a JSON with Id 2, that is, different from what was passed in the parameter.

  1. GET with invalid parameter

GET in the URL localhost:8080/users/search? sex=ABC, that is, an invalid sex for the system (the correct sex would be sex=male or female).

  1. POST of JSON already with id.

Perform a POST in order to persist some data new, but already with the id.

Thanks in advance.

  • Victor, basically you want to know the type of http return most indicated for each of these cases right?

  • Exactly. To do the correct treatment of Status in my application

1 answer

0

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.

  1. 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.

  1. 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:

  1. Ignore the sent in JSON and take the truth of the URL or;
  2. Return an error code 400 as I put above stating the inconsistency.
  1. 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.

  1. 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.