correctness

Parsing Accept & quality values

Ranking types by q-value, breaking ties by specificity and then header order, and respecting q=0. The parsing rules every content-negotiating server needs to get right.

The Accept header is a structured list of preferences, not a string. Implementing negotiation with includes() or startsWith() gets wrong answers on real browsers. Here’s what a correct parser does.

The input

Accept: text/markdown, text/html;q=0.8, */*;q=0.1

Split into entries on ,. Each entry has:

  • A type (possibly with a wildcard, like text/* or */*)
  • Zero or more parameters, separated by ;
  • A quality factor q (default 1 if absent), ranging 0 to 1

The ranking rules

  1. Sort by q descending. Higher-q types are preferred.
  2. Break ties by specificity. Within the same q, a fully specified type (text/markdown) beats a subtype wildcard (text/*), which beats the catch-all */*.
  3. Break remaining ties by header order. When two types you can produce still tie (same q, same specificity), serve the one the client listed first. RFC 9110 gives order no meaning, but you have to pick something, and “HTML wins ties” is also a choice. It’s the one that defeats the request. negotiator (what Express’s accepts uses) applies rules 1–3 in this order: q, then specificity, then header order.
  4. Respect q=0. It explicitly means don’t send me this. Never choose a type the client marked q=0, even if it’s your only supported representation — return 406 Not Acceptable instead.

Equal q is the common case

Three agents on the status page send:

Accept: text/markdown, text/html, */*

Markdown and HTML are both at q=1. A server that serves Markdown only when its q is strictly greater than HTML’s (markdown > html) gives these agents HTML, even though they named Markdown first. Use >= with Markdown preferred on ties, or rules 2 and 3 above. Both serve Markdown for every real agent header we’ve seen.

*/* doesn’t break the tie either. In text/markdown, */*, HTML only reaches q=1 through the wildcard. That says “anything is acceptable,” not “I want HTML as much as Markdown.” The named type wins under rule 2.

The algorithm

Given:

  • A list of types the client accepts (from the header)
  • A list of types your server can produce

Return the type you should serve, or “none” (→ 406).

for each type you can produce:
  find the best-matching accept entry
  (exact type match > text/* match > */* match)
  the score is that entry's q-value
  (0 if no match, or if matched entry had q=0)

pick the type with the highest score
on a tie, prefer the more specific matching entry,
  then the entry listed earlier in the header
if max score is 0, return 406

What libraries do for you

Every mature stack has an Accept parser:

  • Node: the accepts package (what Express uses), or negotiator
  • Flask / Werkzeug: request.accept_mimetypes.best_match([...])
  • Django: request.accepted_types (4.2+), or request.get_preferred_type([...]) (5.2+)
  • Ruby: Rails’ respond_to, but it ignores any Accept containing */* (most agent headers) and doesn’t honor q=0. See the Rails recipe for the workaround. Raw access via Rack::Utils.q_values
  • PHP: Symfony’s HttpFoundation Request::getAcceptableContentTypes
  • Go: github.com/golang/gddo/httputil has NegotiateContentType

Use these. Writing your own parser is how you end up with a bug where Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8 (a real Chrome header) accidentally matches your “Markdown” branch.

Gotchas

  • Missing Accept header: means no constraint. Serve your default (usually HTML). It is not the same as an empty Accept.
  • Accept: */*: means anything is fine. Also serve your default.
  • Accept: text/markdown;q=0: means anything but Markdown. Your server should serve HTML (or whatever else), not 406.
  • Whitespace and case: Accept values are case-insensitive for type names and parameter names. Parameter values are not, in general, but charset=utf-8 is fine either case.

Test vectors

Quick sanity checks your parser should pass:

AcceptServer producesShould serve
text/markdownmd, htmlmarkdown
text/markdown, text/html;q=0.8md, htmlmarkdown
text/markdown, text/html, */*md, htmlmarkdown (Claude Code, Copilot CLI, Microsoft Copilot)
text/markdown, */*md, htmlmarkdown (named type beats wildcard)
*/*, text/markdownmd, htmlmarkdown (named type beats wildcard, even listed later)
text/*, text/markdownmd, htmlmarkdown
*/*, text/htmlmd, htmlhtml
text/html, text/markdownmd, htmlhtml (first listed wins the tie)¹
text/html;q=1.0, text/markdown;q=0.5md, htmlhtml
text/htmlmd, htmlhtml
text/markdown;q=0, text/htmlmd, htmlhtml
text/markdown;q=0md only406
(no Accept)md, htmlhtml (default)
*/*md, htmlhtml (default)²

¹ A server that always prefers Markdown on ties serves Markdown here. That’s also defensible: a browser never names text/markdown, so a client that does is asking for it. The readiness check reports this case but doesn’t score it.

² When both types match the same entry, nothing in the header separates them, so the server’s own order decides. List HTML first in what your server produces. (negotiator and most libraries break this last tie by the order of the list you pass in.)