Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper RESTful response to POST and PUT on nested resources

I'm designing a restful api, and trying to really do it right for the first time.

I've defined some nested resources (comments in a blog post), and that choice mirrors the fact that comments are nested within the blog post document in mongo.

I'm not interested in serving individual comments out of context, so I've deferred implementing GETs for the nested resources. However, it makes sense to implement POSTing to the comments collection, and PUTting to a comment uri.

Specific questions:

1) Does it make sense to respond to a POST with a 201 and a Location header set to the parent resource? If not, how do I communicate the parent location to inform navigation choices on the client?

2) Similar question for PUT, how do I best communicate to the client that it should look to the parent resource to locate its update? (preferably without the client having to make assumptions about my uri scheme). Is the Location header reasonable on a 200?

like image 593
BnWasteland Avatar asked Dec 10 '10 06:12

BnWasteland


1 Answers

Although I have never done it myself, I have heard of people using the Content-Location header for this purpose. Content-Location is used to identify the location of the resource that is represented by the returned entity.

In the case of your PUT and POST, you may not actually want to return the entire blog post, so I'm not sure how valid it is to return a Content-Location header even when you are not returning a representation in the response.

Having said that, I can't think of any negative effects, so here is what I'm suggesting:

PUT /Blog/343/Comment/23
=>
200 OK
Content-Location: /Blog/343


POST /Blog/343/Comments
=>
201 Created
Location: /Blog/343/Comment/24
Content-Location:  /Blog/343
like image 86
Darrel Miller Avatar answered Mar 31 '23 23:03

Darrel Miller