Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what http response code for rest service on put method when domain rules invalid

Tags:

rest

What is the most appropriate response code to return when using the PUT method to update a resource, and the request contains some data that would invalidate the domain rules?

For example, a customer resource must have a name specified. If an agent tries to issue a PUT without supplying a name I don't want to update the resource, and I want to tell the caller that they need to supply a name.

What HTTP response code?

like image 472
rotary_engine Avatar asked Feb 03 '10 09:02

rotary_engine


People also ask

What is the response code for PUT method?

Responses to the PUT method are non-cacheable. PUT requests usually respond back with status code 200.

What is the possible error response code when a post API is requested as a put API?

The 204 status code is usually sent out in response to a PUT , POST , or DELETE request when the REST API declines to send back any status message or representation in the response message's body.

What is a 402 error code?

The HTTP 402 Payment Required is a nonstandard response status code that is reserved for future use. This status code was created to enable digital cash or (micro) payment systems and would indicate that the requested content is not available until the client makes a payment.

When should 422 be used?

In an ideal world, 422 is preferred and generally acceptable to send as response if the server understands the content type of the request entity and the syntax of the request entity is correct but was unable to process the data because its semantically erroneous.


2 Answers

How about 422?

"The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions."

RFC 4918, Section 11.2

like image 78
Julian Reschke Avatar answered Sep 22 '22 04:09

Julian Reschke


The response code is not related to the http method in this case. You should return the same status code as if it had been a POST request. I'd say you should use 400 or 409 (Note: See further discussion of the difference between the two in the comments).

like image 41
troelskn Avatar answered Sep 22 '22 04:09

troelskn