rest - What is the correct way to process nested resources in a RESTful API? -


consider i'm using following restful api endpoints:

  • /users/: show users
  • /users/$user_id/: show specific user
  • /users/$user_id/posts/: show posts user
  • /users/$user_id/posts/$post_id/: show specific post user

constraint in data model: post has user.

by "processing nested resources" mean handling crud operations.

should implement crud operations (post, put, patch, delete) on /users/$user_id/posts/ endpoint or should create endpoint /posts/ , handle crud operations there, while keeping first endpoint read-only?

sorry if question exists in maybe form on so. :-) there's "fud" around restful apis.

thanks in advance tips/clarifications!

kind regards, k.

you should implement operations on existing /posts/ , /posts/$post_id/ endpoints. there's reason make multiple endpoints represent same resources. why make them figure out can on /users/$user_id/posts/$post_id/ have go /posts/$post_id/ delete?

sometimes, people implement as

/users/: show users /users/$user_id/: show specific user /users/$user_id/posts/: show posts user  --  /posts/: show posts                         -- operations /posts/$post_id/: show specific post user    -- operations 

they use /users/$user_id/posts/ non-canonical reference user's posts. while can't call wrong, it's highly preferable stick 1 endpoint per resource. filter parameters not hard.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -