The HTTP 416 status code stands for "Range Not Satisfiable". It is a client error response indicating that the server cannot fulfill a range request specified by the client, because the requested range is outside the size of the target resource or is invalid in some other way.

When is HTTP 416 Used?

  • When a client makes a range request (using the Range header in the request) for a part of the resource, but the server cannot satisfy that range because the range is invalid or out of bounds.
  • It typically happens when the client requests a range that is beyond the size of the resource, such as asking for bytes starting from an offset that exceeds the total content length of the resource.

Common Causes of HTTP 416 Errors

  • Requested range is beyond the resource's size: The client requests a range (e.g., byte range) that is larger than the resource.
  • Invalid range syntax: The range specified by the client is malformed or doesn't conform to the expected format.
  • Range request with no content: If a client requests a range from an empty file or a file that has no content, the server may return a 416 error.

Example Scenarios

  • The client tries to download the last 1 MB of a file that is only 500 KB in size.
  • The client attempts to request a byte range that starts after the end of a file.
  • The client specifies an incorrect range (e.g., bytes=500-200), where the start byte is larger than the end byte.

Example

Client Request (Invalid Range)

    
        GET /video.mp4 HTTP/1.1
        Host: example.com
        Range: bytes=5000-10000
    

(The client requests bytes from 5000 to 10000 of a file, but the file might be smaller than this range, so the request is invalid.)

Server Response (416 Range Not Satisfiable)

    
        HTTP/1.1 416 Range Not Satisfiable
        Content-Type: application/json

        {
          "error": "Requested range is not satisfiable. The file size is smaller than the requested range."
        }
    

Best Practices for Handling HTTP 416

  • Validate range requests: Ensure that range requests sent by clients are within the actual bounds of the resource.
  • Provide useful error messages: Include clear explanations in the response body about why the range request was not satisfied, such as "Range exceeds resource size."
  • Support partial content: If the server supports range requests, handle invalid ranges gracefully, possibly returning the valid portion of the resource or adjusting the requested range.
  • Use the Content-Range header: In the response, provide the Content-Range header that informs the client about the valid range of data available for the resource, especially if the requested range is out of bounds.

Summary

HTTP 416 indicates that the range specified by the client in the Range header is invalid or out of bounds relative to the size of the resource. The client should check that the range is valid and within the size limits of the resource. To resolve this, the client can adjust the range request to fit within the available content.