Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTTP 304 in response to POST

Tags:

I have a REST API that allows modification of resources using HTTP POST. It's possible that a client may submit a POST request that results in no modification of the resource. I'm thinking about using the 304 response generally used for conditional responses to indicate that the request had no effect. I haven't been able to find any examples of this being done, so I figured I'd ask here and see if anyone else is doing this or has an opinion about it.

like image 624
IronDuck Avatar asked Nov 18 '13 22:11

IronDuck


People also ask

Does a 304 response generally contain a message body?

The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.

How does a 304 work?

304 Not Modified is an HTTP status code that is returned to the client when the cached copy of a particular file is up to date with the server. When a client such as a browser stores something in cache, it also keeps the Last-Modified header sent from the server.

Which HTTP status code is usually returned when a resource is cached?

The items with code "200 (cache)" were fulfilled directly from your browser cache, meaning that the original requests for the items were returned with headers indicating that the browser could cache them (e.g. future-dated Expires or Cache-Control: max-age headers), and that at the time you triggered the new request, ...


2 Answers

After some consideration, I've decided to stick to a normal 200 response with the unchanged resource entity. My initial intent was to provide a concise way to indicate to the client that the resource was not modified. As I thought more about it I realized that in order to do anything useful with the 304 response, they would have to already have a cached version and in that case it would be trivial to compare the version of the cached copy with the version returned in a 200 response.

like image 115
IronDuck Avatar answered Sep 20 '22 01:09

IronDuck


I have a REST API that allows modification of resources using HTTP POST. It's possible that a client may submit a POST request that results in no modification of the resource.

HTTP POST in the RESTful approach implies a creation of a resource, not a modification. For modification you should use HTTP PUT.

Solution of your problem is HTTP Status 200 OK when something was modified and HTTP Status 204 No Content when there was no modification. According to:

The common use case is to return 204 as a result of a PUT request, updating a resource, without changing the current content of the page displayed to the user. If the resource is created, 201 Created is returned instead. If the page should be changed to the newly updated page, the 200 should be used instead.

-- MDN web docs


For example:

-- Request
POST /people HTTP/1.1
Content-Type: application/json

{
    "name": "John"
}

-- Response
HTTP/1.1 201 Created
Location: /people/1
-- Request
PUT /people/1 HTTP/1.1
Content-Type: application/json

{
    "name": "John"
}

-- Response
HTTP/1.1 204 No Content
-- Request
PUT /people/1 HTTP/1.1
Content-Type: application/json

{
    "name": "Robert"
}

-- Response
HTTP/1.1 200 OK
Content-Type: application/json

{
    "name": "Robert"
}
like image 42
emstol Avatar answered Sep 17 '22 01:09

emstol