Costa
Costa

Reputation: 654

Rest API, save nested models in one request

As I know in rest we need to save each model in separate request. What if I have 3-4 levels of nested models and would like to save it all in one request, whats the best practice? (Rails, PHP, Node.js)

Upvotes: 1

Views: 68

Answers (1)

Evert
Evert

Reputation: 99533

REST doesn't really talk about models, it talks about resources.

It's fine in REST services for 'some data' (your model) to be represented by multiple resources.

So if you define a new resource that combines all these models into a single larger model, then it would also be acceptable for you to submit a PUT request there and update everything in 1 request, atomically.

One thing to look out for though is caching. If you heavily rely on caching, updating the big resource does not automatically invalidate all the sub-resources in the cache. As far as I know, there's no standard way yet to tell a client that other resources should be expelled from the cache. There's a 2011 draft, but it seems abandoned:

https://datatracker.ietf.org/doc/html/draft-nottingham-linked-cache-inv-04

Upvotes: 1

Related Questions