/ HTTP Headers

Content-Range

The Content-Range header is an essential part of the HTTP protocol, particularly when dealing with partial content requests and responses. In this article, we will delve into the purpose, syntax, and usage of the Content-Range header, with practical examples to help you better understand its role in HTTP communication.

Purpose of the Content-Range Header

The primary purpose of the Content-Range header is to specify the range of bytes being sent in a partial content response or requested in a partial content request. This header enables clients to request specific portions of a resource, such as a large file, and allows servers to send only the requested part, rather than the entire resource. This functionality is particularly useful for resuming interrupted downloads or streaming media content.

Syntax of the Content-Range Header

The Content-Range header follows a specific syntax, as defined by the HTTP/1.1 specification:

Content-Range: bytes <start>-<end>/<total>
  • bytes: The unit of measurement for the range, which is typically “bytes” for binary data.
  • <start>: The starting byte position of the range (inclusive).
  • <end>: The ending byte position of the range (inclusive).
  • <total>: The total size of the resource, in bytes. This value can be an asterisk (*) if the total size is unknown.

Examples of Content-Range Header Usage

Partial Content Request

In this example, a client requests a specific range of bytes from a resource by including the Range header in the request:

GET /large-file.mp4 HTTP/1.1
Host: example.com
Range: bytes=1000-1999

Partial Content Response

In response to the above request, the server sends the specified range of bytes and includes the Content-Range header to inform the client about the range being sent:

HTTP/1.1 206 Partial Content
Content-Type: video/mp4
Content-Length: 1000
Content-Range: bytes 1000-1999/8000

In this example, the server is sending bytes 1000 to 1999 (inclusive) of the 8000-byte resource.

Requesting Unknown Total Size

If a client wants to request a range of bytes without knowing the total size of the resource, it can use the following syntax:

GET /large-file.mp4 HTTP/1.1
Host: example.com
Range: bytes=1000-1999/*

Responding with Unknown Total Size

If the server does not know the total size of the resource, it can respond with an asterisk (*) in the Content-Range header:

HTTP/1.1 206 Partial Content
Content-Type: video/mp4
Content-Length: 1000
Content-Range: bytes 1000-1999/*

Summary

The Content-Range header plays a crucial role in handling partial content requests and responses in the HTTP protocol. By understanding its purpose, syntax, and usage, you can effectively implement partial content handling in your applications, improving the user experience by allowing for resumed downloads and efficient media streaming.

Was this helpful?

Thanks for your feedback!