How to set the User-Agent string in Curl? [Java Code]

You can use the -A or --user-agent command-line option to pass your User-Agent string to Curl. By default, Curl sends its User-Agent string to the server in the following format: "curl/version.number". For example, when you use the Curl tool version 7.54.1, it sends the "User-Agent: curl/7.54.1" HTTP header to the server. In this User-Agent Curl example, we set a custom User-Agent string to the ReqBin echo URL using the -A command line option. Click Run to execute the Curl User-Agent example online and see the results. The Java code was automatically generated for the Curl User Agent example.
How to set the User-Agent string in Curl? [Java Code] Run
curl https://reqbin.com/echo
   -A "ReqBin Curl Client/1.0"
Updated: Viewed: 18264 times
Java code for Curl User Agent example

Java code for Curl User Agent Example

This Java code snippet was generated automatically for the Curl User Agent example.
<< Back to the Curl User Agent example

What is Curl?

Curl (stands for Client URL) is a command-line tool for transferring data to or from the server using URL syntax. Curl allows you to communicate with the server by specifying the target URL and the data you want to send. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, FTPS, and SFTP, has built-in support for SSL certificates, HTTP Cookies, and more. Curl is ideal for testing APIs and for use in automation scenarios. Curl works on platforms Linux, Windows, and macOS.

What is User-Agent?

A user agent is any software acting on behalf of a user that makes a request to a server, receives a response from the server, and processes it. The user agent may have a user interface and interact with the end-user, or it may not have a user interface. In this case, it must provide a procedural or declarative language that can be used to provide a user interface. When the browser connects to the website, it includes the User-Agent string in the HTTP request header. This User-Agent header string varies from browser to browser, but usually includes the name and version of the browser, as well as some information about the operating system it runs on. Web browsers and mobile apps are examples of user agents.

How do I change the default Curl User-Agent string?

The general form of passing a custom User-Agent string to Curl is as follows:

Change Curl User-Agent String
curl -A "product/version" [URL]
curl --user-agent "product/version" [URL]
curl -H "User-Agent: product/version"

Can I remove the User-Agent string from the Curl request?

Yes, you can use the -H command-line option with the User-Agent header name and no content after the ':' sign. This will force Curl to send the request without the User-Agent header (instead of sending it with an empty value).

Remove Curl User-Agent String
curl -H "User-Agent:" [URL]

How to send a custom User-Agent string?

Below is an example of sending a custom User-Agent string to the ReqBin echo URL using the --user-agent command-line parameter:

Custom Curl User-Agent String Example
curl --user-agent "ReqBin/1.0" https://reqbin.com/echo"

Curl User-Agent Examples

The following are examples of the User-Agent string in popular browsers:

Safari User-Agent String

Below is an example of a User-Agent string in Safari:

Safari User Agent String Example
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15

Google Chrome User-Agent String

Below is an example of a User-Agent string in Google Chrome:

Google Chrome User Agent String Example
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36

Mozilla Firefox User-Agent String

Below is an example of a User-Agent string in Mozilla Firefox:

Google Chrome User Agent String Example
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:89.0) Gecko/20100101 Firefox/89.0

See also

Generate code snippets for Java and other programming languages

Convert your Curl User Agent request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the Java code generator.