Activity 30: HTTP Status Codes

DarsieDarsie
8 min read

HTTP status codes are three-digit numbers that indicate the result of an HTTP request. They are grouped into five classes, each serving a specific purpose:

  1. Informational (1xx): These codes provide provisional responses and indicate that the server has received the request and is continuing to process it. The client can expect further communication.

  2. Successful (2xx): These codes indicate that the request was successfully received, understood, and accepted. They are the most common status codes when everything goes as expected.

  3. Redirection (3xx): These codes indicate that further action needs to be taken to complete the request. Typically, they instruct the client to redirect to a different URL or resource.

  4. Client Errors (4xx): These codes indicate that the client seems to have made an error in the request. It could be due to invalid input, authentication issues, or insufficient permissions.

  5. Server Errors (5xx): These codes indicate that the server encountered an error while processing the request. Server errors often result from issues on the server-side, such as server overload or misconfigurations.

1xx (Informational)

These status codes indicate that the request has been received and that the process is ongoing.

  • 100 Continue: This status code tells the client that the initial part of the request has been received and that it should continue sending the remainder of the request.

  • When to use: Use it when you want to acknowledge the client’s request and allow them to continue sending data in the request body. It’s handy for large uploads where you don’t want to interrupt the process

  • Example Response:

      HTTP/1.1 100 Continue
    
  • 101 Switching Protocols: Indicates that the server understands and is willing to comply with the client's request to switch protocols.

  • When to use it: Use it when the client requests to switch to a different protocol, such as moving from HTTP to WebSocket for real-time communication.

  • Example Response:

      HTTP/1.1 101 Switching Protocols
      Upgrade: websocket
      Connection: Upgrade
    
  • 102 Processing (WebDAV): When you see this, it means the server got your request and is working on it, but it doesn’t have an answer for you just yet.

  • When to use it: Use it in cases where the server needs some time to process a request, like when handling complex operations or calculations. It lets the client know that the request was received and is being worked on.

  • Example Response:

      HTTP/1.1 102 Processing
      Date: Tue, 31 Oct 2023 12:45:00 GMT
      Server: Apache/2.4.41 (Ubuntu)
      Allow: OPTIONS, GET, HEAD, POST, PUT, DELETE, PROPATCH
      Content-Length: 0
    
  • 103 Early Hints (Experimental): This one is a bit special. It’s used with a “Link” header to tell the client to start getting ready for some resources even before the server finishes preparing the full response.

  • When to use it: Use it when you want to optimize performance by giving the client a hint about resources they might need based on their initial request. This can be useful for preloading assets like images, stylesheets, or scripts to make the user experience smoother.

  • Example Response:

      HTTP/2 103 Early Hints
      Link: </style.css>; rel=preload; as=style
      Link: </script.js>; rel=preload; as=script
      Link: </image.png>; rel=preload; as=image
    
  • Follow-Up Response:

  • After the 103 hints, the server will eventually send the final response, which might look something like this:

      HTTP/2 200 OK
      Content-Type: text/html
    
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <link rel="stylesheet" href="/style.css">
      </head>
      <body>
          <script src="/script.js"></script>
          <img src="/image.png" alt="Example Image">
      </body>
      </html>
    

2xx (Success)

These codes signify that the client's request was successfully received, understood, and accepted.

  • 200 OK: The request was successful, and the server returned the requested data.

  • For GET requests, the requested data is in the response body.

  • For HEAD requests, you get just the info about the data, no actual data.

  • For PUT or POST requests, the result of the action is in the response.

  • For TRACE requests, you get the request itself as received by the server.

  • When to use: This is the standard response for successful HTTP requests. It means that the request was successful, and the server returned the requested data.

  • Example Response:

      HTTP/1.1 200 OK
      Content-Type: application/json
    
      {
          "message": "Data retrieved successfully!",
          "data": { ... }
      }
    
  • 201 Created: The request was successful, and a new resource was created.

  • When to use: Use it when a resource is successfully created in response to a client’s request.

  • Example Response:

      HTTP/1.1 201 Created
      Location: /resources/123
      Content-Type: application/json
    
      {
          "message": "Resource created successfully!",
          "id": 123
      }
    
  • 204 No Content: The request was successful, but there is no content to send in the response.

  • When to use: Use it when there’s no data to send, but you want to update headers or provide some information.

  • Example Response:

      HTTP/1.1 204 No Content
    

3xx (Redirection)

These status codes indicate that the client must take additional actions to complete the request.

  • 300 Multiple Choices: There are multiple ways to get what you asked for. You need to pick one, but there’s no set way to do it. Think of it like a menu with several options, and you choose what you want.

  • When to use it: Use it when a request can lead to multiple responses, and the client needs to make a choice.

  • 301 Moved Permanently: Indicates that the requested resource has been moved to a new URL.

  • When to use: Use it when a resource has permanently moved to a new location, and you want to tell the client where to find it now.

  • Example Response:

      HTTP/1.1 301 Moved Permanently
      Location: https://new-url.com/resource
    
  • 302 Found: The requested resource resides temporarily under a different URI.

  • When to use: Use it when a resource has temporarily moved, and you expect it to come back to its original location eventually.

  • Example Response:

      HTTP/1.1 302 Found
      Location: https://temporary-url.com/resource
    
  • 303 See Other: We’ve found what you wanted, but it’s somewhere else. Go to that place to get it but use a GET request when you go there.

  • When to use it: Use it when you want the client to get the requested resource at a different location with a GET request.

  • 304 Not Modified: Indicates that the resource has not been modified since the last request, allowing for more efficient caching.

  • When to use it: Use it for caching purposes to tell the client that the resource hasn’t changed, so they can use their cached version.

  • Example Response:

      HTTP/1.1 304 Not Modified
    
  • 305 Use Proxy (Deprecated): In the past, we used to tell you to go through a proxy to get this, but it’s not safe anymore, so don’t do it. It’s like an old, unsafe shortcut that’s not recommended.

  • When to use it: This one’s not used anymore due to security concerns. Avoid it.

  • 306 Unused: This code used to mean something, but we’re not using it now. It’s just reserved in case we need it in the future.

  • When to use it: Don’t use it. It’s not used in the current HTTP specification.

  • 307 Temporary Redirect: Like a temporary detour sign on a road, we’re telling you to go somewhere else for now, but use the same method you used before.

  • When to use it: Use it when you want the client to temporarily go to a different location for the resource but keep using the same HTTP method as before.

  • 308 Permanent Redirect: The thing you want has moved forever. Here’s the new address, and make sure you use the same method as you did before.

  • When to use it: Use it when a resource has permanently moved to a new location, and you want to tell the client where to find it now, while also specifying they should use the same HTTP method.

4xx (Client Error)

These codes indicate that there was an error with the client's request.

  • 400 Bad Request: The request was invalid or cannot be understood by the server.

  • When to use: Use it when the client sends a request that’s malformed or confusing.

    Example Response:

      HTTP/1.1 400 Bad Request
      Content-Type: application/json
    
      {
          "error": "Bad Request",
          "message": "Missing required parameter 'name'."
      }
    
  • 401 Unauthorized: Authentication is required and has failed or has not been provided.

  • When to use: Use it when the client needs to provide authentication (like a username and password) to access a resource.

  • Example Response:

      HTTP/1.1 401 Unauthorized
      Content-Type: application/json
    
      {
          "error": "Unauthorized",
          "message": "Authentication credentials were missing or incorrect."
      }
    
  • 403 Forbidden: The client does not have permission to access the requested resource.

  • When to use: Use it when the client is authenticated but doesn’t have permission to access a specific resource.

    Example Response:

      HTTP/1.1 403 Forbidden
      Content-Type: application/json
    
      {
          "error": "Forbidden",
          "message": "You do not have permission to access this resource."
      }
    
  • 404 Not Found: The requested resource could not be found.

  • When to use it: Use it when the requested resource can’t be found on the server.

  • Example Response:

      HTTP/1.1 404 Not Found
      Content-Type: application/json
    
      {
          "error": "Not Found",
          "message": "The requested resource could not be found."
      }
    

5xx (Server Error)

These status codes indicate that the server encountered an error while processing the request.

  • 500 Internal Server Error: An unexpected condition was encountered by the server.

  • When to use it: Use it when something unexpected goes wrong on the server, and it can’t fulfill the request.

  • Example Response:

      HTTP/1.1 500 Internal Server Error
      Content-Type: application/json
    
      {
          "error": "Internal Server Error",
          "message": "An unexpected error occurred. Please try again later."
      }
    
  • 503 Service Unavailable: The server is currently unable to handle the request due to temporary overloading or maintenance.

  • When to use it: Use it when the server is temporarily unable to handle requests, often due to maintenance or overload. Include a user-friendly message and a “Retry-After” header if possible.

    Example Response:

      HTTP/1.1 503 Service Unavailable
      Content-Type: application/json
    
      {
          "error": "Service Unavailable",
          "message": "The service is temporarily unavailable. Please try again later."
      }
    

    References:

    A Comprehensive Guide to HTTP Status Codes: When and How to Use Them in API Development | by Utkarsh Shukla | Medium

    HTTP Status Codes (restfulapi.net)

0
Subscribe to my newsletter

Read articles from Darsie directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Darsie
Darsie