HTTP status compatibility
I am currently implementing a RESTful API (nothing serious, just for a blog engine I'm developing for fun) and I have some questions about HTTP status compatibility.
To create a new blog post, I have to make a POST request, if all goes well the post will be created and then returned in a format that matches the request.
I read this page from wikipedia about status 200 OK
which
In a POST request, the response will contain an object describing or containing the result of the action
Good. But then there is a status 201 Created
:
The request has been completed and a new resource has been created.
So my question is, when the POST request is successful and a new blog post wan is created, can I send these two http status codes, or just one at a time?
I didn't get this information from the RFC , thought I hadn't read it in full.
I think only one HTTP status is allowed at a time, but then which one should I use?
EDIT (new question): What if the activity is editing an existing blog post? I have a PUT request to a URI and this time I have to send back 200 OK
and then a header Location:
too? Since this location will be exactly the same as the URI of the PUT request, except that it must be a GET request, is this ok?
source to share
All 2xx status is successful. However, in the case of a POST to create a resource, you should probably return 201 along with the location of the resource. From the specification:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.2
The request is fulfilled and as a result a new resource has been created. The generated resource may reference the URI returned in the response entity with the most specific URI for the resource provided by the location of the header field. The answer SHOULD include an object containing a list of resource characteristics and a location (s) from which the user or user agent can select one of the appropriate. The format of the entity is indicated by the media type given in the Content-Type header field. the origin server MUST create the resource before returning a 201 status code. If the action cannot be taken immediately, the server MUST respond with a 202 (Accepted) response.
In other words, you should return:
201 Created
Location: http://www.example.com/path/to/resource
The browser then knows that this is the resource being referenced and that the request was successful at the same time. You don't need to worry about multi-status.
source to share
[I realize this answer is a bit late for the original seeker, but others who might stumble on the question in the future may 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 object if you also set the title Content-Location
:
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 don't include the header Content-Location
, any response body will be interpreted as a hypertext list of resource representations (not a single rep.) For the newly created resource.
source to share