What is the difference between an HTTP request header and a query parameter, and when should each be used to pass information to a Node.js server?
Short answer
Headers carry metadata about the request itself (authentication tokens, content type, accepted formats) that's generally not part of the actual resource being requested, while query parameters are part of the URL and typically represent filtering, sorting, or pagination options…
Advertisement
AModel answer
Headers carry metadata about the request itself (authentication tokens, content type, accepted formats) that's generally not part of the actual resource being requested, while query parameters are part of the URL and typically represent filtering, sorting, or pagination options for the specific resource being requested -- a general convention is that headers carry cross-cutting concerns applicable across many different endpoints, while query parameters carry endpoint-specific data relevant to that particular request.
// Header: cross-cutting concern (auth), same pattern across many endpoints
fetch('/api/orders', { headers: { Authorization: 'Bearer token123' } });
// Query parameters: specific to this endpoint's filtering/pagination needs
fetch('/api/orders?status=shipped&page=2&limit=20');Real-world example
An API consistently uses an Authorization header for authentication across every single endpoint (since it's a cross-cutting concern), while using endpoint-specific query parameters like ?status=shipped for filtering results, following a clear, predictable convention rather than mixing the two purposes inconsistently.
Likely follow-ups
- Why is it generally considered bad practice to put sensitive data like an API key in a query parameter rather than a header?
- How would you decide whether a piece of data belongs in a route parameter (like /orders/:id) versus a query parameter?
Related topics