Api Design easy
You're designing a REST API for a blog. Which endpoint best represents 'get all comments for post 123'?
- A GET /comments?postId=123
- B GET /posts/123/comments
- C POST /comments/fetch { "postId": 123 }
- D GET /posts/123?include=comments
Answer: B. GET /posts/123/comments
In RESTful API design, resources should be named using nouns and organized hierarchically to represent relationships. The path `/posts/123/comments` naturally implies 'all comments belonging to the post with ID 123'. Avoid using verbs (like 'get' or 'fetch') in URLs, as the HTTP method (GET, POST, etc.) explicitly defines the action being performed.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- A client wants to update only the 'status' field of an order, leaving other fields unchanged. Which HTTP method is most appropriate?
- A client calls POST /users with valid data, and the user is created. What status code should the server return?
- A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- An API returns 10,000 products. Returning all in one response is impractical. Which pagination approach is simple and stateless?
- Your API serves both mobile apps and third-party integrations. Which authentication method is most appropriate?
- Service A calls Service B, which is failing. Without protection, A keeps sending requests, cascading the failure. What pattern prevents this?