HTTP Status Codes
What are Status Codes?
All REST requests return a status code as part of the HTTP response that informs the client of the request result. HTTP defines numerous standard status codes that are shown as a subset of numerical codes, ranging from 100 to 599 depending on the type of status that is conveyed. If you’re using cURL to make a request, you can pass the -I option to return the status code to the terminal. In this section, we’ll show you how to identify a successful REST API request and deal with some of the more common errors that might occur while working with one.
200 – OK
This indicates that the REST API successfully carried the requested action and that no more action is necessary. A 200 response should also include a response body that is dependent on the method used in the request. For example, GET will return an entity that corresponds to the requested resource, and POST will return an entity that describes or contains the result of an action.
4xx – Client-Side Errors
The 400-499 code range, indicates problems on the client side, i.e. the request the client made isn’t valid. Let’s take a look at some of the more common client side errors you might encounter while working with REST APIs.
400 – Bad Request
400 is the generic client-side error status that can be the result of a range of problems including malformed request syntax, invalid request message parameters, and incorrect request routing. When this error occurs, the client should make modifications to the request before sending it to the REST API again.
401 – Unauthorized
A 401 error code indicates that the client didn’t have sufficient privileges to operate on the requested resource.This is typically the result of inaccurate or nonexistent access credentials. The client should modify the request to include suitable authentication information in the request.
403 – Forbidden
A 403 error response indicates that the client’s request is formed correctly, but the REST API refuses to honor it. This is typically the result of the client not having the necessary permissions for the resource. This differs from a 401 error because it means the client is authenticated, but isn’t allowed to access the requested resource with the provided method.
404 – Not Found
The 404 error status code indicates that the REST API can’t map the client’s URI to a resource. In other words, the API can’t find the requested resource. No indication is given as to whether the condition is temporary or permanent and subsequent client requests can be permissible because the resource might become available. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.
405 – Method Not Allowed
405 errors indicate that the client tried to use an HTTP method that the resource does not allow. For instance, a read-only resource might only support GET, so requests to PUT, POST, or DELETE would return this error. If you encounter this error, modify your request to a method the resource allows.
429 – Too Many Requests
A 429 error occurs when a client attempts too many requests within a certain timeframe. This error is triggered based on rate limit settings the REST API service provider has set, and this is used to prevent their servers from being overloaded. To avoid this issue, you should make yourself aware of any rat
500- Internal Server Error
Most REST APIs use 500 status codes whenever the request handler raises an exception. This represents a problem with the REST API server and it’s possible that simply retrying the request can resolve this problem. If the problem persists, you should contact the REST API provider to further diagnose the issue.