REST - Creating Nested Resources with single POST

Deadly 提交于 2019-12-04 05:16:34

Yes, there is nothing wrong with creating sub-resources at the same time you create the parent resource. It would even be OK to use PUT instead of POST to do this, as everything under the parent URL is part of/belongs to the resource you are uploading.

EDIT:

Now I want to allow the creation of a notice with a photo directly attached, enabling the creation of /notices/{notice} and /notices/{notice}/photos/{photo} with a single POST request to /notices/{notice}/photos/{photo}

This I disagree with. I suggest POSTing to the collection resource's URL, /notices. You provide the notice and its photos as a single representation (request body). The back end will then create resources for both the notice and any constituent photographs.

Although its essential in many cases, multiple edits/creates are not formally addressed by the RESTful architecture style.

Problem starts when you need to report failures on part of the collection, and the problem is worsen when they failures have different cause.

An it will effect choosing the right Hypermedia controls which are essential for the client to find a way forward in the given transaction/conversation.

so my suggestion is to have a nested cycle or POST requests rather than a sing POST to create nested Resources , so that it'd be easier and clearer to address each Resource state change.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!