Some servers or APIs require specific HTTP headers to grant access or provide content in the desired format. wget can send custom headers, enabling flexible and tailored requests for testing, automation, or integration with services that rely on specialized headers.

By adding User-Agent, Authorization, or other fields, you can simulate browser clients, authenticate with tokens, or request special formats. Multiple headers can be combined in a single request, allowing precise control over the interaction with the server.

This customization ensures compatibility with varied server requirements and fosters better automation. When combined with other wget features, custom headers enhance flexibility and power in script-based workflows.

Steps to add custom headers in wget:

  1. Add a single custom header with the --header option.
    $ wget --header="User-Agent: MyTestApp/1.0" http://example.com/api
  2. Add multiple headers by repeating the --header option.
    $ wget --header="User-Agent: MyTestApp/1.0" --header="Authorization: Bearer abc123" http://example.com/api

    Add as many headers as needed for authentication, formatting, or testing.

  3. Use --debug to inspect the headers sent.
    $ wget --header="User-Agent: MyTestApp/1.0" --debug http://example.com/api

    --debug output shows the exact request sent, assisting in troubleshooting header issues.

Discuss the article:

Comment anonymously. Login not required.