Custom request headers are often the fastest way to reproduce an API call, probe server-side filtering, or match a download endpoint that expects extra request metadata. In wget, header injection keeps the entire workflow inside one CLI tool instead of shifting into a browser or a separate HTTP client just to add one or two fields.
The mechanism is simple: repeat --header for every extra field that needs to be sent. Wget passes those header lines as written, which makes the option useful for auth headers, trace IDs, custom accept values, and other request-level controls that change how a remote service responds.
Header values often carry secrets, routing hints, or behavior toggles. Quote the full header string, verify the final request shape before blaming the server, and avoid verbose debug output on shared systems when the headers contain credentials or session material.
Steps to send custom headers with wget:
- Send one custom header and confirm that the remote endpoint receives it exactly as written.
$ wget -qO- \ --header='X-Test: demo-123' \ https://httpbin.org/headers | jq -r '.headers["X-Test"]' demo-123
One --header option adds one complete header line, including the field name and the value after the colon.
- Add multiple headers by repeating --header in the same command.
$ wget -qO- \ --header='Accept: application/json' \ --header='X-Trace-Id: demo-1234' \ https://httpbin.org/headers | jq .headers { "Accept": "application/json", "Accept-Encoding": "identity", "Host": "httpbin.org", "User-Agent": "Wget/1.25.0", "X-Amzn-Trace-Id": "Root=1-69c5ba4f-4f9c28e0201520cd497ea792", "X-Trace-Id": "demo-1234" }
Repeating the option is clearer than trying to combine unrelated headers into one string, especially when values include commas, spaces, or semicolons.
- Inspect the final request block with --debug when the server response still looks wrong.
$ wget --debug \ --header='Accept: application/json' \ --header='X-Trace-Id: demo-1234' \ --spider https://httpbin.org/headers 2>&1 \ | sed -n '/request begin/,/request end/p' ---request begin--- HEAD /headers HTTP/1.1 Host: httpbin.org User-Agent: Wget/1.25.0 Accept: application/json Accept-Encoding: identity Connection: Keep-Alive X-Trace-Id: demo-1234 ---request end---
Debug output exposes every header that leaves the host, so do not archive or share it when the request includes credentials, cookies, or bearer tokens.
- Clear previously added user-defined headers by passing an empty --header before rebuilding the final set.
$ wget -qO- \ --header='X-Test: old-value' \ --header='' \ --header='X-Test: new-value' \ https://httpbin.org/headers | jq -r '.headers["X-Test"]' new-value
An empty --header resets earlier user-defined headers in the current command, which helps when a long shell alias or copied command adds the wrong values.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
