views:

98

answers:

3

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?

A: 

That is correct, you will only be sending one HTTP status. You should return 200, unless your response also includes a URI which the client could request if it so desires.

mjv
Okay thanks, but then when should be used the 201 status code?
p4bl0
+6  A: 

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.

AlBlue
Perfect answer, thanks!
p4bl0
But then I have another question, please see my edit.
p4bl0
You might want to consider using 303 as well.
D.Shawley
Oh yep, thanks D.Shawley!
p4bl0
A: 

Something i found today that might be useful to someone else : HTTP headers status graph for REST.

p4bl0