When a client makes an HTTP request with a specific byte range, it expects the server to return a partial response containing the requested bytes. This is particularly useful when downloading large files, resuming interrupted downloads, or streaming media content. However, there are cases when the server cannot fulfill the requested range, either because the range is invalid or because the resource does not support byte range requests. In such cases, the server returns a 416 status code.
Reasons for a 416 Status Code
There are two primary reasons why a server might return a 416 status code:
- Invalid byte range: The requested byte range is invalid, either because it is syntactically incorrect or because it exceeds the bounds of the resource. For instance, if a client requests bytes 500-1000 of a 400-byte resource, the server will return a 416 status code.
- Resource does not support byte range requests: Some resources, such as dynamically generated content or encrypted content, may not support byte range requests. In these cases, the server will return a 416 status code, even if the requested range is valid.
Example Request and Response
Consider a scenario where a client requests a byte range that exceeds the bounds of the resource. The following example demonstrates this situation:
Request
GET /large-file.mp4 HTTP/1.1
Host: example.com
Range: bytes=5000-10000
In this example, the client requests bytes 5000-10000 of the file “large-file.mp4” from the server.
Response
HTTP/1.1 416 Range Not Satisfiable
Content-Type: text/html
Content-Range: bytes */500
The server responds with a 416 status code, indicating that the requested range cannot be satisfied. Additionally, the server provides a Content-Range
header with an asterisk (*) instead of the actual byte range, which signifies that the requested range is not valid. The server also specifies the total size of the resource (500 bytes) to inform the client about the correct bounds.
Handling a 416 Status Code
When a client receives a 416 status code, it should take the following steps:
- Verify that the requested byte range is valid and within the bounds of the resource. If the range is incorrect, the client should adjust the request accordingly.
- If the requested range is valid, consider the possibility that the server does not support byte range requests for the requested resource. In this case, the client should request the entire resource without specifying a byte range.
Summary
In summary, the HTTP status code 416, “Range Not Satisfiable,” occurs when the server cannot fulfill a client’s request for a specific byte range of a resource. This can happen due to an invalid byte range or because the resource does not support byte range requests. To handle a 416 status code, clients should verify the requested range and adjust their requests accordingly, or request the entire resource if necessary.