HTTP Status Codes

HTTP status codes are essential components of the communication process between clients (such as browsers) and servers. They provide valuable feedback on the success or failure of an HTTP request, helping both developers and users understand how their request has been processed.

What Are HTTP Status Codes?

HTTP status codes are three-digit numbers returned by the server in response to an HTTP request. These codes indicate the result of the request and are classified into five categories:

  1. 1xx: Informational
  2. 2xx: Success
  3. 3xx: Redirection
  4. 4xx: Client Error
  5. 5xx: Server Error

Each category serves a different purpose and provides different levels of information about the request’s outcome. These codes are critical for both developers and users to understand whether their request was successful, if there were issues with the client-side, or if the server faced problems.

1xx: Informational Status Codes

These codes indicate that the server has received the request and is processing it. The response typically doesn’t contain much information and is often used for temporary conditions.

  • 100 Continue: This status code is sent by the server to tell the client that the initial part of the request has been received and the client should continue with the rest of the request. Example: When uploading a large file, the server might send a 100 Continue response to indicate it is ready to receive the body of the request.

2xx: Success Status Codes

A 2xx status code means that the request was successfully received, understood, and accepted by the server. These are the most common status codes you’ll encounter.

200 OK: This is the most commonly used status code and means that the request was successful, and the server returned the requested data.
Example: When you visit a webpage, the server responds with a 200 OK status code if everything is functioning normally.

HTTP/1.1 200 OK
Content-Type: text/html

201 Created: The request was successful, and a new resource has been created as a result.
Example: After posting data to create a new user profile in an application, the server will return 201 Created to confirm the new resource has been created.

HTTP/1.1 201 Created
Content-Type: application/json

204 No Content: The server has successfully processed the request, but there is no content to send in the response.
Example: A successful DELETE request where the resource has been deleted but no further data is returned.

3xx: Redirection Status Codes

Redirection status codes indicate that the client must take additional action to complete the request. The server is redirecting the client to another resource.

301 Moved Permanently: This code indicates that the resource has been permanently moved to a new location. The client should use the new URL in the future.
Example: When a website permanently changes its domain, a 301 redirect is used to send users to the new domain.

HTTP/1.1 301 Moved Permanently
Location: https://newdomain.com/

302 Found: This is a temporary redirect, where the requested resource has been temporarily moved to a different location. The client should continue to use the original URL for future requests.

4xx: Client Error Status Codes

4xx status codes indicate that the client has made an error, such as a malformed request or requesting a resource that doesn’t exist.

400 Bad Request: This code indicates that the server could not understand the request due to invalid syntax.
Example: When a user submits a form with missing required fields, the server may respond with a 400 Bad Request error.

HTTP/1.1 400 Bad Request
Content-Type: application/json

401 Unauthorized: The client must authenticate before making the request. This error occurs when the server requires authentication, and the client hasn’t provided valid credentials.
Example: When trying to access a private page without logging in, the server responds with a 401 Unauthorized error.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Restricted Area"

404 Not Found: This error indicates that the server could not find the requested resource.
Example: When trying to access a webpage that no longer exists, the server will return a 404 Not Found error.

HTTP/1.1 404 Not Found

5xx: Server Error Status Codes

5xx status codes indicate that the server failed to fulfill a valid request. These errors are generally caused by server-side issues.

500 Internal Server Error: This is a generic error message indicating that the server encountered an unexpected condition that prevented it from fulfilling the request.
Example: When there is a malfunction or misconfiguration on the server, the server may return a 500 Internal Server Error.

HTTP/1.1 500 Internal Server Error

502 Bad Gateway: This error occurs when a server acting as a gateway or proxy receives an invalid response from the upstream server.
Example: If you’re accessing a site through a proxy server, and the proxy fails to fetch data from the original server, it may return a 502 Bad Gateway error.

HTTP/1.1 502 Bad Gateway

503 Service Unavailable: This code indicates that the server is temporarily unavailable, often due to overload or maintenance.
Example: When a website is down for maintenance, it may return a 503 Service Unavailable error.

HTTP/1.1 503 Service Unavailable
Retry-After: 3600

Leave a Comment

BoxofLearn