Which HTTP status codes should a REST API use?
Review common status codes for successful requests, client errors, authentication, and server failures.
Answer
HTTP status codes communicate the outcome of a request. A consistent status-code strategy makes an API easier to use, debug, and integrate with other services.
Common success codes include 200 OK for a successful request, 201 Created when a resource is created, and 204 No Content when the request succeeds but there is no response body. Common client-error codes include 400 Bad Request, 401 Unauthorized for missing or invalid authentication, 403 Forbidden when the caller lacks permission, and 404 Not Found when a resource cannot be found.
Use 500 Internal Server Error for unexpected server failures. Avoid returning 200 for every outcome or exposing stack traces and sensitive implementation details in error responses.
Key points to remember
- Use 201 Created for successful resource creation when appropriate.
- Distinguish authentication (401) from authorization (403).
- Return a consistent, documented error format.
- Never expose secrets or internal stack traces in production responses.
Keep practising
Understanding beats memorization.
Explain the concept in your own words, then think through how you would use it in a real project.
AI-powered mock interview practice is planned for a future release.