An Extensive Guide to Deciphering HTTP Status Codes in APIs

Agarwal Peeyush
3 min readJan 16, 2024

--

Photo by Jorge Rosal on Unsplash

Introduction:

Three-digit numbers known as HTTP (Hypertext Transfer Protocol) status codes are given back by a server to a client in response to their request. It is essential for developers and users to comprehend these status codes in the context of APIs (Application Programming Interfaces). We’ll explore the meaning of different HTTP status codes and how they affect API connection in this thorough tutorial.

  1. The Basics of HTTP Status Codes:

HTTP status codes are grouped into five categories, each represented by the first digit of the code:

  • 1xx (Informational): Request received, continuing process.
  • 2xx (Success): The action was successfully received, understood, and accepted.
  • 3xx (Redirection): Further action needs to be taken to complete the request.
  • 4xx (Client Error): The request contains bad syntax or cannot be fulfilled.
  • 5xx (Server Error): The server failed to fulfill a valid request.

2. Common HTTP Status Codes in APIs:

  • 200 (OK): The standard response for a successful HTTP request. The payload contains the requested data.
  • 201 (Created): Indicates that the request has been fulfilled and has resulted in one or more new resources being created.
  • 202 (Accepted): Indicates that the request has been accepted for processing, but the processing has not been completed yet.
  • 204 (No Content): The server successfully processed the request but there is no additional content to send in the response.
  • 400 (Bad Request): The request could not be understood or was missing required parameters.
  • 401 (Unauthorised): Authentication is required, and the user has not provided valid credentials.
  • 402 (Payment Required): It is a nonstandard response status code that is reserved for future use. This status code was created to enable digital cash or (micro) payment systems and would indicate that the requested content is not available until the client makes a payment.
  • 403 (Forbidden): Authentication is required, and the user has not provided valid credentials.
  • 404 (Not Found): The requested resource could not be found on the server.
  • 405 (Method Not Allowed): Indicates that the server knows the request method, but the target resource doesn’t support this method.
    The server must generate an Allow header field in a 405 status code response. The field must contain a list of methods that the target resource currently supports.
  • 422 (Unprocessable Entity): Used for validation errors or malformed requests where the server understands the content but cannot process it.
  • 500 (Internal Server Error): A generic error message indicating that an unexpected condition was encountered on the server.
  • 502 (Bad Gateway): Indicates that the server, while acting as a gateway or proxy, received an invalid response from the upstream server.
  • 503 (Service Unavailable): Indicates that the server is not ready to handle the request.
  • 504 (Gateway Timeout): Indicates that the server, while acting as a gateway or proxy, did not get a response in time from the upstream server that it needed in order to complete the request.

3. Best Practices For Handling HTTP Status Codes:

  • Provide Descriptive Error Messages:
    Provide a detailed error message in the response when an error happens to aid developers in debugging the problem.
  • Use Appropriate Status Codes:
    Select status codes that appropriately indicate how the request was fulfilled. For Example: Use 201 for resource generation and 404 for resources not found.
  • Leverage Redirection Codes Wisely:
    When appropriate, use redirection codes (3xx) to direct clients to take further action.
  • Implement Rate Limiting:
    Notify clients that they have gone over the permitted rate limit by using status code 429 (Too Many Requests).

4. Advanced Concepts:

  • Hypermedia as the Engine of Application State (HATEOAS):
    Promote the usage of HATEOAS to give details on the possible courses of action depending on the application’s present state.
  • Custom Status Codes:
    Consider defining custom status codes for specific scenarios in your API to enhance clarity and specificity.

Conclusion:

Understanding HTTP status codes is fundamental for building and consuming APIs. They provide as an essential channel of communication between clients and servers and reveal the status of a request. Through adherence to industry best practices and the right use of status codes, developers may build reliable APIs that gracefully manage errors and facilitate effective user communication. Whether you are a provider or consumer of APIs, mastering HTTP status codes is key to navigating the intricacies of web development and ensuring a seamless user experience.

Happy Learning! Feel free to connect with me over LinkedIn!

--

--

No responses yet