Fetch part of a file with HTTP Range requests
An HTTP Range request asks a server for selected byte positions of a representation with GET instead of the whole resource. If supported, the server can return 206 Partial Content with Content-Range; otherwise it may return the entire body with 200. Range requests help resume downloads or retrieve media segments, but clients must verify what they actually received.
An HTTP Range request asks a server for selected byte positions of a representation with GET instead of the whole resource. If supported, the server can return 206 Partial Content with Content-Range; otherwise it may return the entire body with 200. Range requests help resume downloads or retrieve media segments, but clients must verify what they actually received.
Request one range
This requests the first 1,024 bytes, positions 0 through 1023 inclusive:
GET /large-file.bin HTTP/1.1
Range: bytes=0-1023A single-range response can look like this:
HTTP/1.1 206 Partial Content
Content-Range: bytes 0-1023/146515
Content-Length: 1024Range positions start at zero and include both ends. Content-Length here is the size of this response range, not the whole file. Content-Range identifies the range within the complete representation and its total length.
Resume safely
Before appending a downloaded segment, make sure the server representation has not changed. Save its ETag or Last-Modified validator and send one in If-Range with the next request. If it still matches, the server can return 206 for the requested range. If it does not, the server may ignore Range and return the current full representation with 200. Replace the partial file; do not append that full response to old bytes.
Range: bytes=1024-
If-Range: "saved-etag"Use a strong ETag or a date validator in If-Range, not both. A client may send Range even without seeing Accept-Ranges, and seeing Accept-Ranges does not guarantee every later response will be partial.
Handle the response
206: Check Content-Range and body length, then store bytes at the matching offset.
200: This is the complete response. Replace the partial file instead of appending.
416: The requested range is not satisfiable for the current representation. The RFC example uses Content-Range: bytes */total-length; recheck the saved offset and resource version.
One Range header can request multiple ranges. The response then uses multipart/byteranges with a separate Content-Range for each part. Prefer a single range unless multiple segments are useful because multipart parsing adds complexity and load. A server may ignore Range or send only some requested ranges, so inspect status and headers. Byte positions refer to the selected representation; content coding can affect which bytes those positions address.
Sources
[RFC 9110 §§14.2–14.4, 15.3.7, 15.5.17](https://www.rfc-editor.org/rfc/rfc9110.html#section-14.2)
[MDN HTTP range requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Range_requests)