Is it RESTful to create complex objects in one POST?

I have a form in which users create Person records. Each person can have several attributes - height, weight, etc. But they can also have lists of related data, such as interests, favorite movies, etc.

I have one form where all this data is collected. It seems to me that I should DELIVER all this data in one request. But is it RESTful? My reading suggests that interests, favorite movies, and other lists should be added to separate POST requests. But I don’t think it makes sense, because one of them may fail, and then there will be a partial insertion of the Personality, and this may be the absence of their interests or favorite films.

+6
source share
3 answers

I do not think that there is a problem with adding all the data to one request, if it is inherently connected with the main resource (i.e. the person in your case). If interest, fav. movies, etc. are own resources, they should also be treated as such.

+2
source

I would say that it completely depends on the targeting and uniqueness of the dependent data.

If your user-related data is user-specific (i.e., an “excellent” string, such as an attribute, such as a string representing the (unapproved) movie name), then it should be included in the POST creation of the user's presentation; however, if the data is independent of the user (where the data can be addressed independently of the user, for example, a link, for example, a movie from a set of films), then it must be added independently.

The rationale for this is that adding links complete with the original POST implies a transaction; that is, if another user removes the movie link for the “favorite” movie between when it is selected on the client and when POST passes, the user adds (by this design) a failure, whereas if the “favorite” movie is not associative, but is simply an attribute with which nothing can be done (attributes (presumably) cannot be annulled by a third party).

And again, this is very important for your specific needs, but I fall on the resolution side of partial inserts and indicate failures. The right way to handle such things if you really don't want to allow partial inserts is to simply implement transactions on the back panel; they are the only way to really deal with a situation where a critical related resource is deleted in the middle of the process.

+4
source

The real limitation in REST is that for the modifiable resource you are GET, you can also go around and set the same view back to change its state. Or POST. Since it’s wise (and very common) to use GET resources, which are great links to other things, it’s also wise to use large packages of things.

Think of resources in REST very broadly. They can match database rows with each other, but they don’t need it. An addressable resource may embed other addressable resources or include links to them. As long as you do your presentation and semantics of the basic protocol operations (e.g. HTTP GET POST PUT, etc.), REST has nothing to do with other design considerations that can make your life easier or harder.

+3
source

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


All Articles