REST vs RPC Example 1 - herougo/SoftwareEngineerKnowledgeRepository GitHub Wiki
Example as a ChatGPT Prompt
I have the following API input-output specifications. Write a RESTful URL spec for each.
1) input: post ID, output: the post corresponding to the input ID
2) input: username, output: all posts with the author corresponding to the user with that username
3) input: user ID, output: all posts in the feed (posts from accounts the user is following) of that user
ChatGPT Response
GET /posts/{postId}GET /users/{username}/postsGET /users/{userId}/feed
Follow-up Question
What if I want to have GET /users/{userId}/posts in addition to GET /users/{username}/posts. How do you resolve ambiguity?
ChatGPT Response
Approaches
- Use Query Parameters:
GET /users/posts?userId=12345vsGET /users/posts?username=johndoe - Use Different Parent Routes:
GET /user-ids/{userId}/postsvsGET /usernames/{username}/posts - Use a Smart Identifier System:
GET /users/{identifier}/posts, where identifier is determined to be id or username - Explicit Prefix in the Path:
GET /users/id/{userId}/postsvsGET /users/name/{username}/posts
Recommendation: #3 is most RESTful, but #4 is the safest.
ChatGPT Response to "Use RPC Instead"
GET /getPost?postId=12345GET /getPostsByUsername?username=johndoeGET /getUserFeed?userId=67890