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

  1. GET /posts/{postId}
  2. GET /users/{username}/posts
  3. GET /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

  1. Use Query Parameters: GET /users/posts?userId=12345 vs GET /users/posts?username=johndoe
  2. Use Different Parent Routes: GET /user-ids/{userId}/posts vs GET /usernames/{username}/posts
  3. Use a Smart Identifier System: GET /users/{identifier}/posts, where identifier is determined to be id or username
  4. Explicit Prefix in the Path: GET /users/id/{userId}/posts vs GET /users/name/{username}/posts

Recommendation: #3 is most RESTful, but #4 is the safest.

ChatGPT Response to "Use RPC Instead"

  1. GET /getPost?postId=12345
  2. GET /getPostsByUsername?username=johndoe
  3. GET /getUserFeed?userId=67890