← All notes

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

Claims

Sources

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.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, net/http.ServeContent; WHATWG, Fetch Standard. BSD-3-Clause; CC-BY-4.0.

Cite this

Permanent ID: iil:note:21vmhkwx

iinventedlight. 2026. "Serve HTTP range requests with Go ServeContent." iinventedlight Library, iil:note:21vmhkwx, version of 2026-11-19. https://iinventedlight.com/library/notes/http-range-requests/. License: CC-BY-4.0.

@misc{iil_note_21vmhkwx,
  author = {{iinventedlight}},
  title = {{Serve HTTP range requests with Go ServeContent}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/http-range-requests/},
  note = {Permanent ID iil:note:21vmhkwx. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/http-range-requests.md.