HTTP response status codes

HTTP response status codes

ยท

4 min read

HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Status codes are issued by the server in response to the client's request made to the server. A status code is a number higher than 100 and lesser than 600 that is part of an HTTP response. HTTP uses these standard status codes to convey the results of a client's request. The responses or status codes are grouped into five categories:

  • 1xx: Informational (100-199) - These are informational status codes. They tell the client that the initial part of the request has been received and the server will respond or they tell the client that the request will fail.

  • 2xx: Success(200-299) - Indicate that the client's request was accepted successfully.

  • 3xx: Redirection(300-399) - Indicates that the client must take further action in order to complete their request.

  • 4xx: Client errors(400-499) - This is a bad request that indicates that the client did something wrong. They are all about invalid requests a client has sent to the server. The client should confirm whether the input is correct before retrying the request.

  • 5xx: Server errors(500-599) - Internal server error. It indicates that the API did something wrong. The server takes responsibility for these errors. The server failed to fulfill an apparently valid request.

When implementing the HTTP status codes in a REST application, we should include at least three codes which are;

  1. 200 - OK(all is well)
  2. 400 - Bad request(Client did something wrong)
  3. 500 - Internal Server error(the API did something wrong)

Status codes to use for a CRUD operation.

CRUD defines the most basic API operations such as Create, Read, Update and Delete. The following are some of the status codes that can be used for CRUD operations.

Success

  • 200 OK. Tells the client that everything went well. It is especially used on successful GET requests and PUT/PATCH requests for updated content.

  • 201 Created. The request has succeeded and a new resource has been created as a result. The server must create the resource before returning the 201 status code. If the action cannot be carried out immediately, the server should respond with a 202(Accepted) response instead. It is used especially for PUT and POST requests.

  • 202 Accepted. It's used for asynchronous processing. A 202 response is typically used for actions that take a long while to process. It tells the client that the request was valid, but the processing has not been completed. The response should indicate the current request status and an estimation of when the user can expect the request to be fulfilled. It's returned when an asynchronous operation is performed and provides a URL for status monitoring.

  • 204 No Content. It's used for updates that don't return data to a client. The request succeeded but there is nothing to show for example saving an edited document. It's usually sent after a successful PUT/DELETE request.

  • 206 Partial Content. It is usually used for paginated resources. The response code is used when the range header is sent from the client to request only part of a resource.

Client Errors

400 Bad requests. This is a general error for a request that can not be processed. Here the server could not understand a particular request due to invalid syntax.

401 Unauthorized. The error response indicates that the client tried to work on a resource without providing proper authorization.

403 Forbidden. The error response indicates that the client does not have access to the requested resource. Unlike 401, the client's identity is known to the server.

404 Not Found. The error response is returned when the server cannot find the requested resource. The server can also send this response instead of 403 to hide an existing resource from an unauthorized client.

405 Method Not Allowed. The client tried to use an HTTP method that the resource does not allow for example a GET request on a form that requires data to be presented via a POST request.

406 Not Acceptable. The response is sent when the server does not find the content that conforms to the criteria given by the user agent that is to say the client requested the data to be returned in a particular format but the server can not return the data in that format.

Server Error

500 Internal Server Error. This is a generic server error. The problem occurred on the server and the client cannot do anything about it.

Conclusion

For more information about HTTP response status codes, check the MDN Docs.

Until next time, thanks for reading!!