wasAsked.

What are the most important HTTP status codes in REST APIs and when do you use them?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Software Engineering

Community answers

Posted by wasAsked editorial collection

Status codes tell clients what happened with their request. Using them correctly makes APIs predictable and easier to integrate. 2xx — Success - 200 OK: successful GET, or PUT/PATCH that returns the updated resource. - 201 Created: a resource was created (usually POST). Include a Location header pointing to the new resource and often the resource in the body. - 202 Accepted: the request was accepted for asynchronous processing (long-running jobs). Return a status URL to poll. - 204 No Content: success with no response body — typical for DELETE or updates that return nothing. 3xx — Redirection - 301 Moved Permanently / 308 Permanent Redirect: the resource has a new permanent URL (308 keeps the HTTP method). - 302 Found / 307 Temporary Redirect: temporary redirect. - 304 Not Modified: the client's cached version (sent with If-None-Match or If-Modified-Since) is still valid; no body is returned. 4xx — Client errors - 400 Bad Request: malformed request or validation failure (invalid JSON, missing required fields). Return details, ideally as Problem Details with field errors. - 401 Unauthorized: the client is not authenticated or the token is invalid or expired. Include a WWW-Authenticate header. (Despite the name, it means "unauthenticated".) - 403 Forbidden: authenticated, but not allowed to perform this action. - 404 Not Found: the resource does not exist (also used to hide resources the user is not allowed to know about). - 405 Method Not Allowed: the endpoint exists but does not support this HTTP method. - 406 Not Acceptable: the server cannot produce the requested format (Accept header). - 409 Conflict: the request conflicts with the current state — duplicate unique value, optimistic concurrency conflict, invalid state transition. - 410 Gone: the resource existed but was permanently removed. - 412 Precondition Failed: a conditional request failed (If-Match ETag no longer matches). - 413 Content Too Large: request body too large. - 415 Unsupported Media Type: wrong Content-Type (for example XML sent to a JSON-only endpoint). - 422 Unprocessable Content: syntactically valid request that violates business rules (some APIs use 400 for all validation errors; choose one convention and apply it consistently). - 429 Too Many Requests: rate limit exceeded; include Retry-After. 5xx — Server errors - 500 Internal Server Error: unexpected server failure. Never leak stack traces; return a correlation or trace ID. - 501 Not Implemented: functionality not supported. - 502 Bad Gateway: a gateway or proxy received an invalid response from an upstream server. - 503 Service Unavailable: temporary overload or maintenance; include Retry-After. - 504 Gateway Timeout: an upstream server did not respond in time. Examples by operation - GET /orders/42: 200 with the order, 404 if missing, 304 if unchanged. - POST /orders: 201 with Location /orders/43, 400 or 422 for invalid data, 409 for duplicates. - PUT /orders/42: 200 or 204, 404, 409 or 412 for version conflicts. - DELETE /orders/42: 204, or 404 (some APIs return 204 even when already deleted, for idempotency). Best practices - Never return 200 with an error flag in the body; use the correct status code. - Use a consistent error format (RFC 9457 Problem Details: type, title, status, detail, instance, plus extensions such as errors and traceId). - Distinguish 401 from 403, and 400 or 422 from 409. - Document the status codes each endpoint can return in OpenAPI.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics