---
source: https://iinventedlight.com/library/notes/http-range-requests.md
title: "Serve HTTP range requests with Go ServeContent"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go docs); CC-BY-4.0 (WHATWG); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Go docs, net/http.ServeContent; WHATWG, Fetch Standard. BSD-3-Clause; CC-BY-4.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: HTTP range requests and Go ServeContent"
tags: [http, range, ServeContent, golang, if-range, 206, 416]
client: Librarian
sources:
  - id: S1
    title: "net/http.ServeContent"
    publisher: Go docs
    url: https://pkg.go.dev/net/http#ServeContent
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Fetch Standard"
    publisher: WHATWG
    url: https://fetch.spec.whatwg.org/
    accessed: 2026-11-19
    license: "CC-BY-4.0"
---
# Serve HTTP range requests with Go ServeContent
**Answer:** `ServeContent` replies to a request for file content. `ServeContent` replies to the request using the content in the provided `ReadSeeker`. Its main benefit over a plain copy is that it handles Range requests properly, sets the MIME type, and also handles if-match, if-unmodified-since, if-none-match, if-modified-since, and if-range. In the Fetch Standard, a range status is a status that is 206 or 416.

## What ServeContent needs

The content's `Seek` method must work. `ServeContent` seeks to the end of the content to determine its size. A File value implements the `ReadSeeker` interface. If no Content-Type header is set on the response, `ServeContent` first tries to deduce the type from the name file extension. If that fails, it reads the first block of the content and passes it to `DetectContentType`. The name is otherwise unused. It can be empty, and it is never sent in the response.

## Validators

If `modtime` is not the zero time or Unix epoch, `ServeContent` puts it in a Last-Modified header. If the request has an if-modified-since header, `ServeContent` uses `modtime` to decide if the content should be sent. If the caller has set an ETag header formatted per RFC 7232, section 2.3, ServeContent then uses that header for if-match, if-none-match, or if-range.

| Range request | Invalid range |
|---|---|
| Range request | Handles Range requests against the `ReadSeeker` |
| Invalid range | Responds with an error message |
| Error headers | By default, strips cache-control, content-encoding, ETag, and last-modified |

If an error occurs when serving the request, such as an invalid range request, `ServeContent` responds with an error message. The `GODEBUG` setting `httpservecontentkeepheaders=1` causes `ServeContent` to preserve these headers.

Fetch also lists if-range when it discusses cache mode, together with if-modified-since, if-none-match, if-unmodified-since, and if-match.

## See also

- [http-status-codes](https://iinventedlight.com/library/notes/http-status-codes/)
- [go-context](https://iinventedlight.com/library/notes/go-context/)
- [node-http-and-url](https://iinventedlight.com/library/notes/node-http-and-url/)

## Claims

- id: C1
  claim: ServeContent handles Range requests and related validators.
  source: S1
  evidence: "it handles Range requests properly, sets the MIME type"

- id: C3
  claim: An ETag on the response is used for If-Range and If-None-Match.
  source: S1
  evidence: "uses it to handle requests using If-Match, If-None-Match, or If-Range"

- id: C4
  claim: Fetch treats 206 and 416 as range statuses.
  source: S2
  evidence: "range status is a status that is 206 or 416"

## Sources

- [S1] net/http.ServeContent. Go docs. https://pkg.go.dev/net/http#ServeContent . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] Fetch Standard. WHATWG. https://fetch.spec.whatwg.org/ . License: CC-BY-4.0. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, net/http.ServeContent; WHATWG, Fetch Standard. BSD-3-Clause; CC-BY-4.0. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
