1001Ferramentas
🔗 Dev

Link Header Parser

Decompose an HTTP Link header (RFC 8288) listing each URI with its parameters (rel, type, title). Useful for paginated APIs and Web Linking.

The Link header: relations in the header instead of the HTML

The Link header expresses in HTTP the same thing the link element expresses in HTML: a relation between the current resource and another. The advantage is that it works for responses that are not HTML — a JSON API can declare next and previous pages without inventing a field in the body, and that is exactly what the GitHub API does.

Parsing holds a trap the page handles: entries are separated by commas, but commas also appear inside URLs and inside quoted parameters. Splitting the header on commas without accounting for that cuts an entry in half, and the result is a truncated link that looks valid. Correct splitting ignores commas inside angle brackets and inside quotes.

The most used parameters are rel, which names the relation; type, which anticipates the target's media type; and title. rel is the only one required in practice, and its values come from an IANA registry — next, prev, first, last for pagination; preload and preconnect for performance; canonical and alternate for SEO.

Frequently asked questions

Link in the header or in the HTML?
For an HTML response both work and count the same, but the header arrives earlier: the browser reads it without waiting for the body, which matters for preload and preconnect. For a response that is not HTML, the header is the only option.
How does pagination with Link work?
The server returns next, prev, first and last relations pointing at complete URLs, and the client just follows them — with no parameter assembly and no knowledge of the pagination scheme. It is the approach that keeps clients working when an API moves from offset to cursor, because the URL arrives ready.
Can you declare canonical via the header?
You can, and it is the way to do it for a PDF, an image or any file without HTML where there is nowhere to put the tag. Search engines honour it. Just do not use both with different values in the same response, because then the outcome depends on who is reading.

Related Tools