The HTTP Status Code you need to know.

Tram Ho

Working with the web means that you are exposed to HTTP responses. Whether you spend primarily on Client or Server, you are often familiar with common status codes such as 200, 404 or 500.

What is HTTP Status Code?

HTTP Response Status Code is a set of standardized and uniformly defined information about http responses.

Status Code consists of 3 digits and is divided into 5 categories, each of which is sorted by the first digit of the number. You will see that these abbreviations are the first number followed by two “x” characters. For example: “4xx”.

  • 1xx: Informational (provides information about requests in progress)
  • 2xx: Successful (indicates that the request has been received and accepted)
  • 3xx: Redirection (indicates that the request must be redirected)
  • 4xx: Client Error (error on the client side)
  • 5xx: Server Error (server-side error).

Common Response Codes

200 (OK)
Everything is wonderful! Code 200 is a sign that the request was successful.

The important thing to keep in mind is that a successful request does not always have the same meaning from a user perspective. A search query against the API can return 200, even if the response does not contain a match.

301 (Moved Permanently)
Although you may not see this too often when making requests to third party APIs, it is very useful for configuring the server to handle migrated resources. For example, refactoring the url of a web page from page.site.com to site.com/page can use 301 to indicate to search engines, browsers and dependent resources that the page still exists. now, but it has a new position.

401 (Unauthorized)
Use when request cannot be processed because of invalid information. If you encounter this code often, make sure your login credentials are correct.

403 (Forbidden)
Not to be confused with code 401, cdde 403 is for known clients on the server, but does not have proper permissions to access resources. Code 403 can occur when the server or API has an approved client list (for example, domain name or IP address) that does not match the client that made the request.

404 (Not Found)
So popular that non-programmers also realize it, 404 is used when the requested resource cannot be found. Perhaps it has moved and no navigation is provided. The URL provided may simply be incorrect. 404 does not provide any indication whether this is a temporary or permanent problem.

408 (Request Timeout)
When the client sends a request to the server and takes too long or the server decides to close the connection instead of continuing to wait, the 408 code is sent. Some servers will send this on idle connections , even if the client has not yet sent the request.

429 (Too Many Requests)
If you use a third-party API in your application, you may encounter code 429. This happens when you reach the scale limit by sending too many requests in a period of time. This time frame and limits will vary depending on the API, but the response will usually include details about the limit and usually the Retry-After header with the time you need to wait before making another request.

500 (Internal Server Error)
Code 500 occurs when the server encounters an unexpected error. This is the default code, to use when there is no more suitable code.

502 (Bad Gateway)
When the server you are connecting to acts as a gateway or proxy, but receives an invalid request from the server it is trying to reach (the origin server). This can happen with standard proxies or even API Gateways. The gateway is working, but the origin server it is trying to reach is having problems.

503 (Service Unavailable)
Code 503 means the server is temporarily unavailable. This can be caused by resource overload, temporary maintenance or any situation where the exact problem is not related to the request. The expectation with response 503 is that the service will be available soon.

504 (Gateway Timeout)
Similar to 502, when the server is a proxy or gateway and has not received a response from the origin server within a reasonable time, it will return code 504. Although the root of the problem will not be clear from this response. , that means one of the dependency resources is having issues with which you might want to check the gateway.

These codes are standardized for a reason. As an API user, it allows you to make assumptions about the response it receives. As the API creator, it gives you a way to interact with API users. Take advantage of this code to make API development and use more consistent.

 

Reference: link

Share the news now

Source : Viblo