User-Agent is one of the HTTP headers used by web servers to understand the request’s origin. It contains information about client browser, version and operating system. The server may respond differently depending on user agent information. Sometimes you may need to set user agent while sending request to server. You can easily do this using cURL tool. In this article, we will learn how to set user agent with cURL.
How to Set User Agent with Curl
Here are the steps to set user agent with cURL.
1. Default User Agent
By default, when you send request using cURL tool, it sends user agent information as ‘curl/version.number’ format. For example, if you are using cURL 7.9.2 to send requests, then it will set user agent as ‘curl/7.9.2’
2. Change User Agent with cURL
Here is the syntax to set user agent with cURL command.
$ curl -A "user-agent-name-here" [URL]
Here is an example to set user agent as Firefox 80.
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/80.0" https://example.com
You can also set user agent with –user-agent option.
curl --user-agent "user-agent-name-here" url
Here is an example to set user agent using -H option. In this case, you specify which HTTP header is to be set.
curl -H "User-Agent: user-Agent-Name-Here"
Setting User agent is a useful to test how your server responds to different browsers, without actually installing all of them.
You can use these steps to simulate request headers for testing purposes, or otherwise.
In this short article, we have learnt how to set user agent using cURL.