Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it RESTful to create complex objects in a single POST?

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

I have a single form where all this data is collected. To me it seems like I should POST all of this data in a single request. But is that RESTful? My reading suggests that the interests, favorite movies and other lists should be added in separate POST requests. But I don't think that makes sense because one of those could fail and then there would be a partial insert of the Person and it may be missing their interests or favorite movies.

like image 558
aw crud Avatar asked Jun 07 '11 12:06

aw crud


People also ask

What makes a website RESTful?

RESTful web services support total client-server separation. They simplify and decouple various server components so that each part can evolve independently. Platform or technology changes at the server application do not affect the client application.

What makes RESTful?

In order for an API to be considered RESTful, it has to conform to these criteria: A client-server architecture made up of clients, servers, and resources, with requests managed through HTTP.

When should I use RESTful API?

The most common scenario of using REST APIs is to deliver static resource representations in XML or JSON. However, this architectural style allows users to download and run code in the form of Java applets or scripts (such as JavaScript).


1 Answers

I'd say that it depends entirely upon the addressability and uniqueness of the dependent data.

If your user-associated data is dependent upon the user (i.e., a "distinct" string, e.g. an attribute such as a string representing an (unvalidated) name of a movie), then it should be included in the POST creation of the user representation; however, if the data is independent of the user (where the data can be addressed independently of the user, e.g. a reference, such as a movie from a set of movies) then it should be added independently.

The reasoning behind this is that reference addition when bundled with the original POST implies transactionality; that is, if another user deletes the movie reference for the "favorite" movie between when it is chosen on the client and when the POST goes through, the user add will (should by that design) fail, whereas if the "favorite" movie is not associative but is just an attribute, there's nothing to fail on (attributes (presumably) cannot be invalidated by a third party).

And again, this goes very much to your specific needs, but I fall on the side of allowing the partial inserts and indicating the failures. The proper way to handle this sort of thing if you really want to not allow partial inserts is to just implement transactions on the back end; they're the only way to truly handle a situation where a critical associated resource is removed mid-process.

like image 54
Paul Sonier Avatar answered Nov 15 '22 22:11

Paul Sonier