Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST: Correct HTTP Response Code For a POST Which Is Ignored

Tags:

We have a REST API which clients routinely POST and PUT data to. When they do this, sometimes they POST data which results in no change on our system. The POSTs and PUTs are well-formed, but they data they're sending is identical to the data in our database. When this happens, I've just found out that we're returning a 400 HTTP status. Unfortunatly, this means "bad request" as in "request could not be understood by the server due to malformed syntax".

Clearly this is not the case, but I'm told that we're going to use this since there's no other appropriate status code. Choices we've considered:

  • 304 Not Modified. This, regrettably, is only for GET requests.
  • 204 No Content. Seems close, but forbids an entity-body.

Other choices seem equally bad. We might go with 200 OK and have the relevant information in the XML document we return, but this doesn't seem very "RESTish". How does the REST world generally handle this?

(Fixed Not Modified response code. Thanks Mkoeller)

like image 572
Ovid Avatar asked Nov 12 '08 13:11

Ovid


People also ask

What is a 402 response?

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.

Can post return 204?

From the service's perspective, a 204 (No Content) response may be a perfectly valid response to a POST, PUT or DELETE request.

What is a 204 error?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page. This might be used, for example, when implementing "save and continue editing" functionality for a wiki site.

What is the proper rest response code for a valid request but an empty data?

304 (Not Modified) This status code is similar to 204 (“No Content”) in that the response body must be empty.


1 Answers

I think it's perfectly fine to return a 200 OK in that case, the data was correctly processed and the server did what it had to. Because the server processed correctly the data, it should return an OK status code. The fact that it ignored it internally is or should be irrelevant.

What the server did to the data should not be told to the clients, they should be told what happened to the request (processed ok, error occurred, and the like).

And if, for some weird reason (I can't think a valid one, btw), it is of interest to the clients, you have the response to tell them so.

like image 161
Vinko Vrsalovic Avatar answered Sep 21 '22 21:09

Vinko Vrsalovic