REST API Status Code Example
Introduction
HTTP status codes are essential in REST APIs as they indicate the outcome of client requests. These standardized codes help clients understand whether their request was successful, requires further action, or encountered an error. Below is an overview of key HTTP status codes categorized by their respective classes.
1xx: Informational Responses
These status codes indicate that the request has been received and is being processed.
2xx: Success Responses
Successful responses indicate that the request was received, understood, and accepted.
200 (OK)
The request was successful, and the server returned the requested data. The response body depends on the HTTP method:
- GET: Returns the requested resource.
- HEAD: Returns headers without a response body.
- POST: Returns a description of the result.
- TRACE: Returns the received request message.
201 (Created)
Indicates that a new resource has been created. The response includes a Location header specifying the URI of the newly created resource.
202 (Accepted)
Indicates that the request has been accepted for processing but is not yet complete. The response may include status information or a pointer to a status monitor.
204 (No Content)
The request was successful, but no content is returned. Often used for PUT, POST, or DELETE requests.
3xx: Redirection Responses
These status codes indicate that further action is required to complete the request.
301 (Moved Permanently)
The requested resource has been permanently moved to a new URI. The Location header contains the new URI.
302 (Found)
The resource is temporarily moved. The client should use the URI in the Location header but continue using the original URI for future requests.
303 (See Other)
Indicates that the response can be retrieved from another URI using a GET request. Commonly used after POST operations.
304 (Not Modified)
Indicates that the requested resource has not changed since the last request. No response body is included, saving bandwidth.
307 (Temporary Redirect)
Similar to 302, but ensures the HTTP method remains unchanged during redirection.
4xx: Client Error Responses
Indicate issues with the client’s request.
400 (Bad Request)
The request is malformed or contains invalid parameters. The client must modify the request before retrying.
401 (Unauthorized)
Authentication is required, or provided credentials are invalid.
403 (Forbidden)
The request is valid, but the client lacks necessary permissions.
404 (Not Found)
The requested resource is not found. The client can retry if the resource might be available later.
405 (Method Not Allowed)
The resource does not support the HTTP method used. The response includes an Allow header listing supported methods.
406 (Not Acceptable)
Indicates that the server cannot produce a response matching the client’s Accept header.
412 (Precondition Failed)
Indicates that one or more preconditions in the request headers were not met.
415 (Unsupported Media Type)
The server does not support the request’s Content-Type.
5xx: Server Error Responses
Indicate problems on the server side.
500 (Internal Server Error)
A generic error indicating an unexpected server issue. Clients can retry the request.
501 (Not Implemented)
Indicates that the server does not support the requested functionality.
Understanding these status codes is essential for effectively working with REST APIs, ensuring smooth communication between clients and servers.