The 206 Partial Content
HTTP status code is sent by the server when it successfully processes a range request, meaning that the server is returning only a portion of the requested resource. This is particularly useful for resuming interrupted downloads or for streaming media files.
When to Use 206 Partial Content
The 206 Partial Content
status code is used in the following scenarios:
- When a client sends a request for a specific range of bytes within a larger resource, such as resuming a paused download.
- When a client requests multiple non-overlapping ranges within a single request, such as video streaming with a custom player.
Range Requests
To request a specific range of bytes, the client includes the Range
header in the request. The Range
header specifies the desired byte range, using a zero-based index.
The basic syntax for the Range
header is as follows:
Range: bytes=start-end
Where start
is the first byte position and end
is the last byte position of the desired range.
Example Request with Range Header
Here’s an example of a request with the Range
header:
GET /large-file.mp4 HTTP/1.1
Host: example.com
Range: bytes=1000-2000
In this example, the client is requesting the bytes from 1000 to 2000 (inclusive) of the large-file.mp4
resource.
206 Partial Content Response
When the server successfully processes the range request, it will return a 206 Partial Content
status code, along with the requested byte range in the response body. The server will also include the Content-Range
header in the response, indicating the range of bytes being sent and the total size of the resource.
Example Response with 206 Partial Content
Here’s an example of a response to the previous request:
HTTP/1.1 206 Partial Content
Date: Wed, 21 Oct 2023 07:28:00 GMT
Server: Apache/2.4.1 (Unix)
Last-Modified: Fri, 09 Aug 2023 23:54:35 GMT
ETag: "e0023-4a5-58e2adeb64d80"
Accept-Ranges: bytes
Content-Length: 1001
Content-Range: bytes 1000-2000/10001
Content-Type: video/mp4
...binary data...
In this example, the server sends the requested byte range (1000-2000) and indicates the total size of the resource (10001 bytes). The Content-Length
header specifies the size of the partial content being sent (1001 bytes).
Multiple Ranges in a Single Request
Clients can also request multiple non-overlapping byte ranges within a single request. To do this, specify multiple ranges separated by commas in the Range
header:
Range: bytes=start1-end1,start2-end2,...
Example Request with Multiple Ranges
Here’s an example of a request with multiple byte ranges:
GET /large-file.mp4 HTTP/1.1
Host: example.com
Range: bytes=1000-2000,3000-4000
In this case, the client is requesting two separate byte ranges: 1000-2000 and 3000-4000.
Example Response with Multiple Ranges
When the server processes a request with multiple byte ranges, it will return a 206 Partial Content
status code, and the response body will contain the requested byte ranges, each preceded by a boundary string and a set of headers.
Here’s an example of a response to the previous request:
HTTP/1.1 206 Partial Content
Date: Wed, 21 Oct 2023 07:28:00 GMT
Server: Apache/2.4.1 (Unix)
Last-Modified: Fri, 09 Aug 2023 23:54:35 GMT
ETag: "e0023-4a5-58e2adeb64d80"
Accept-Ranges: bytes
Content-Length: 2034
Content-Type: multipart/byteranges; boundary=3d6b6a416f9b5
Connection: close
--3d6b6a416f9b5
Content-Type: video/mp4
Content-Range: bytes 1000-2000/10001
...binary data...
--3d6b6a416f9b5
Content-Type: video/mp4
Content-Range: bytes 3000-4000/10001
...binary data...
--3d6b6a416f9b5--
In this example, the server sends the two requested byte ranges, each preceded by a boundary string (--3d6b6a416f9b5
) and a set of headers indicating the Content-Type
and Content-Range
.
Summary
The 206 Partial Content
HTTP status code is used when a server successfully processes a range request, returning only a portion of the requested resource. This is particularly useful for resuming interrupted downloads or for streaming media files. Clients can request specific byte ranges using the Range
header, and the server will respond with the requested byte ranges and a Content-Range
header indicating the range of bytes being sent and the total size of the resource.