The User-Agent header identifies the client’s software, device, or platform. By default, cURL announces itself, but changing this header can simulate various browsers or clients.

Altering the User-Agent broadens testing scenarios, enabling examination of server responses tailored to mobile devices, different browser versions, or other user agents. It also helps bypass simple filters that reject default cURL identifiers.

By customizing the user agent, you can explore conditional content delivery, test compatibility, and ensure the server meets diverse client requirements.

Steps to change the user agent string in cURL:

  1. Open a terminal.
  2. Check the default User-Agent with --verbose.
    $ curl "http://www.example.com" --verbose
    > User-Agent: curl/8.1.2
  3. Change the User-Agent using --user-agent.
    $ curl --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0" "http://www.example.com" --verbose
    > User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0

    Mimicking a browser’s user agent can produce different server responses.

  4. Alternatively, set the User-Agent with --header.
    $ curl --header "User-Agent: Mozilla/5.0 ..." "http://www.example.com" --verbose

    Using --header is another method to alter headers.

  5. Permanently define a User-Agent in ~/.curlrc.
    $ echo 'user-agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; ...)"' >> ~/.curlrc

    All future cURL requests use this user agent by default.

  6. Verify the changed User-Agent in subsequent requests.
    $ curl "http://www.example.com" --verbose
    > User-Agent: Mozilla/5.0 ...

    Confirm that the desired user agent is now sent.

Discuss the article:

Comment anonymously. Login not required.