Let's say that I am tracking recipes, ingredients, and authors. I have a form which a user can fill out to create a recipe, add and/or link the appropriate ingredients, and then add and/or link the authors. So if I was to do this separately it could be 1 POST to /recipes to create the recipe returning the id. Then 1 or many POSTs to /ingredients to create the necessary ingredients. Then 1 or many POSTS to /recipe/some_recipe_id/ingredients to add each ingredient to the recipe. And then repeat for authors. However, what if any of these requests fails? Could I instead implement this as 1 POST request to /recipes and have it do all the necessary inserts?
-
Sure you can, but why doesn't you support POSTing an author or ingredients when adding them? Then a POST for a recipe would refer to existing resources instead. – Smutje Jul 16 '19 at 14:50
-
So in the recipe form, users can create ingredients and then link them but then what if they user creates ingredients but then decides not to proceed with finishing the recipe. Then there could be ingredients that just never get used. And also how would I link the ingredients if there is not existing recipe (and thus recipe_id) yet? I could create the ingredient but I would still have to wait until after the recipe creation to create the link, right? – rahc01 Jul 16 '19 at 14:55
-
If you read through [the definition of POST](https://tools.ietf.org/html/rfc7231#section-4.3.3) you will see that a server will process a request according its own semantics which allows you to basically do everything that you want or need. The only thing the HTTP spec requires is that you return the URI of a created resource via the `Location` HTTP header. It gets interesting when multiple resources are created though. HTTPs strength are, for sure, not the processing of batch requests though. The general rule of thumb here is: how would you do it on the Web and just reuse the same concept – Roman Vottner Jul 16 '19 at 17:50
-
[This](https://stackoverflow.com/questions/29092787/http-post-response-location-header-when-creating-multiple-resources) might also be interesting to you then – Roman Vottner Jul 16 '19 at 17:54
-
I'm not understanding your last sentence "how would you do it on the Web and just reuse the same concept". Could you reword or elaborate on that? – rahc01 Jul 16 '19 at 19:30
1 Answers
Could I instead implement this as 1 POST request to /recipes and have it do all the necessary inserts?
Yes, absolutely.
There is nothing that says that changes to one resource cannot also have side effects on other resources.
HTTP is an application protocol whose application domain is the transfer of documents over a network (Webber, 2011); a web server is fundamentally an http compliant document store. So sending an entire document to the server to be stored is consistent with the semantics of the messages.
If you review the create semantics described in the POST specification, you'll see that the protocol supports the creation of more than one resource; the server is expected to return identifiers for all of the created resource (in some unspecified way presumably constrained by the media type).

- 1
- 1

- 52,766
- 5
- 49
- 91