/ HTTP Headers

If-Range

The If-Range HTTP header is used to conditionally request a specific range of a resource, based on the given entity-tag (ETag) or date. This header is particularly useful when a client wants to download a part of a resource, but only if the resource hasn’t changed since the client last accessed it.

Purpose of the If-Range Header

The primary purpose of the If-Range header is to allow clients to efficiently download partial content of a resource, without the risk of receiving an outdated or inconsistent version. This is especially useful for resuming interrupted downloads or streaming media content.

When a client sends an If-Range header in a request, it indicates that the server should only send the requested range if the resource has not been modified since the specified date or ETag. If the resource has been modified, the server should send the entire resource instead of the requested range.

Syntax of the If-Range Header

The If-Range header can take either an entity-tag (ETag) or a date as its value. The general syntax for the header is as follows:

If-Range: <etag>

or

If-Range: <date>

Using Entity-Tag (ETag)

An ETag is a unique identifier assigned by the server to a specific version of a resource. When using an ETag with the If-Range header, the client requests a range of the resource only if the ETag matches the current version on the server. If the ETag does not match, the server sends the entire resource.

Using Date

When using a date with the If-Range header, the client requests a range of the resource only if the resource has not been modified since the specified date. If the resource has been modified since the given date, the server sends the entire resource.

Examples of If-Range Header Usage

Example 1: Requesting a Range with Matching ETag

In this example, the client requests a specific range of a resource, providing an ETag that matches the current version on the server.

Request:

GET /file.pdf HTTP/1.1
Host: example.com
Range: bytes=1000-1999
If-Range: "123456789"

Since the ETag matches, the server responds with the requested range.

Response:

HTTP/1.1 206 Partial Content
Content-Type: application/pdf
Content-Range: bytes 1000-1999/10000
ETag: "123456789"

[Partial content of the file]

Example 2: Requesting a Range with Non-Matching ETag

In this example, the client requests a specific range of a resource, providing an ETag that does not match the current version on the server.

Request:

GET /file.pdf HTTP/1.1
Host: example.com
Range: bytes=1000-1999
If-Range: "987654321"

Since the ETag does not match, the server responds with the entire resource.

Response:

HTTP/1.1 200 OK
Content-Type: application/pdf
ETag: "123456789"

[Entire content of the file]

Example 3: Requesting a Range with Date

In this example, the client requests a specific range of a resource, providing a date that precedes the last modification date of the resource.

Request:

GET /file.pdf HTTP/1.1
Host: example.com
Range: bytes=1000-1999
If-Range: Tue, 15 Nov 1994 08:12:31 GMT

Since the resource has been modified since the specified date, the server responds with the entire resource.

Response:

HTTP/1.1 200 OK
Content-Type: application/pdf
Last-Modified: Wed, 16 Nov 1994 09:10:00 GMT

[Entire content of the file]

Summary

The If-Range HTTP header is a powerful tool for requesting partial content of a resource, ensuring that clients receive the most up-to-date version. By using either an ETag or a date as the conditional value, clients can efficiently download or resume interrupted transfers without the risk of receiving outdated or inconsistent data. Understanding the purpose, syntax, and usage of the If-Range header is essential for optimizing web performance and providing a seamless user experience.

Was this helpful?

Thanks for your feedback!