When making requests using cURL, web servers identify the request source based on a value known as the “User-Agent” string. By default, cURL uses its name and version as the User-Agent. However, in some situations, you may need to imitate a browser or another tool's request or test how servers respond to various User-Agent strings.

Web servers and applications often deliver content based on the User-Agent. For example, a website might present a mobile-optimized version of its content to requests coming from smartphone browsers. Modifying the User-Agent string in cURL allows developers and testers to mimic requests from different clients, aiding in debugging and website behavior analysis.

You can fake the cURL User-Agent string by using the -A or –user-agent option followed by the desired User-Agent string.

Related: advanced-usage \ Related: change-user-agent

Steps to modify the cURL User-Agent string:

  1. Open the terminal.
  2. Make a default request with cURL to a website.
    $ curl https://www.example.com/
  3. Modify the User-Agent to imitate a Mozilla Firefox browser request.
    $ curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0" https://www.example.com/
  4. Use the longer –user-agent option to achieve the same result.
    $ curl --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Firefox/68.0" https://www.example.com/
  5. Check server's response for content or headers that may vary based on User-Agent.
    $ curl -I --user-agent "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7" https://www.example.com/

    The -I flag sends a HEAD request, which will only return headers, allowing you to observe possible variations in server responses based on User-Agent.

  6. Always return to the default cURL User-Agent string or a legitimate User-Agent for standard usage, as custom User-Agent strings might violate some website's terms of service.
    $ curl https://www.example.com/
Discuss the article:

Comment anonymously. Login not required.