Question

I'm currently implementing a RESTful API (nothing serious, just for a blog engine i'm developping for fun) and i've some questions about HTTP status compatibility.

To create a new blog post i have to do a POST request, if every thing goes fine, the post is created and then returned in the format corresponding to the request.

I read on this page from wikipedia about 200 OK status that

In a POST request the response will contain an entity describing or containing the result of the action

Okay. But then there is the 201 Created status:

The request has been fulfilled and resulted in a new resource being created.

So my question is: when a POST request is successful and a new blog post is created wan i send back these two http status code or only one at a time is allowed?

I didn't get this info from the RFC, thought i didn't read it entirely.

I'm thinking that only one HTTP status at a time is allowed but then which one should i use?

EDIT (new question): What if the action is editing an existing blog post? I have a PUT request on a URI and this time i'll have to send back 200 OK and then a Location: header too? Because this location will be exactly the same as the URI of the PUT request, except that it should be a GET request, is that okay?

Was it helpful?

Solution

All 2xx status are succesful. However, in the case of a POST to create a resource, you probably should return a 201 along with a location to the resource. From the spec:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2

The request has been fulfilled and resulted in a new resource being created. The newly created resource can be referenced by the URI(s) returned in the entity of the response, with the most specific URI for the resource given by a Location header field. The response SHOULD include an entity containing a list of resource characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. The origin server MUST create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server SHOULD respond with 202 (Accepted) response instead.

In other words, you should return:

201 Created
Location: http://www.example.com/path/to/resource

The browser will then know that is the resource to refer to, and that the request has been successful, at the same time. You do not need to worry about multi-status.

OTHER TIPS

[I realise this answer is a bit late for the original asker, but others who may stumble across the question in the future might be interested]

In addition to what AlBlue said:

In other words, you should return:

201 Created
Location: http://www.example.com/path/to/resource

You can also return the newly created entity, as long as you also set the Content-Location header:

POST /make-new-resource

then

HTTP/1.1 201 Created
Location: http://www.example.com/path/to/resource
Content-Location: http://www.example.com/path/to/resource

[representation of new resource]

If you do not include a Content-Location header, any response body will be interpreted as a hypertext list of resource representations (rather than a single rep.) for the newly create resource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top