The correct RESTful answer for POST and PUT on nested resources

I am developing a sedative api and am trying to do it right the first time.

I have identified some of the embedded resources (comments in a blog post), and this choice reflects the fact that the comments are embedded in the blog document in mongo.

I'm not interested in serving individual comments from the context, so I deferred the implementation of GET for nested resources. However, it makes sense to embed POSTing in the comment collection and PUTting in the uri comment.

Concrete questions:

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

2) A similar question for PUT, what is the best way to tell the client that it must look for the parent resource in order to find its update? (preferably if the client should not make assumptions about my uri scheme). Is the location header reasonable at 200?

+8
source share
1 answer

Although I never did this myself, I have heard of people using the Content-Location header for this purpose. Content-Location is used to determine the location of the resource represented by the returned object.

In the case of your PUT and POST, you may not want to return the entire blog post, so I'm not sure how true the return of the Content-Location header is, even if you do not return the view in response.

Having said that, I can’t think of any negative consequences, so here is what I suggest:

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 
+12
source share

Source: https://habr.com/ru/post/650015/


All Articles