12 Essential Curl Commands for Linux, Windows and macOS [Java Code]
Curl is a popular command-line utility for transferring data to or from a server using over 25+ protocols. The Curl command-line tool provides several advanced options such as user authentication, proxy support, resuming transmission, limiting bandwidth and transfer rates, and more. Curl commands work without user interaction and are therefore ideal for use in automation scenarios. This article will go over the 12 most essential Curl commands for day-to-day use for making requests over HTTP/HTTPS protocols. The Java code was automatically generated for the Curl Commands example.
Then check Curl by executing the following command:
Check Curl version
To use Curl on Windows, download the installer from the Curl official website and unpack the curl.zip archive to the desired local folder on your computer. Add the Curl folder (usually C:\Curl\bin) to your Windows PATH environment variable to invoke the Curl command from anywhere else. Enter curl --version on the command line to make sure you can use Curl on Windows.
Curl comes with macOS already. You can update Curl to the latest version by installing macOS Homebrew Software Package Manager. After installing Homebrew, open a Terminal and type:
Install Curl on macOS
brew install curl
How to use the Curl command-line tool?
The Curl command syntax is protocol-dependent. For the HTTP protocol, the Curl command line syntax is as follows:
curl [options] [URL]
Below is a list of the 12 best Curl commands that you will use the most during your work.
1. Get resource content by URL
Curl command to get the content of a page
If no parameters are specified, Curl sends an HTTP GET request to the server and outputs the resource's content (for example, the HTML code of a page) to standard output (usually a terminal window).
The list of URLs is passed to Curl with one of the -o or -O command-line options. As we saw in example #2, for the command line parameter -o, you can pass the file name under which the resource will be saved, and the -O command line parameter tells Curl to save the downloaded resource with the original file name.
5. Check Page HTTP headers
Curl command to check page HTTP headers
curl -I https://reqbin.com/echo
The -I parameter tells Curl to send an HTTP HEAD request to the server instead of a GET. The HEAD request is similar to a GET, except that the server only returns HTTP headers. This is useful if you only want to check the URL headers and not load the page content (to save Internet traffic).
6. Force Curl to use HTTP/2 protocol
Curl command to use HTTP/2 protocol
curl --http2 https://reqbin.com
The --http2 option forces Curl to use the HTTP/2 protocol instead of HTTP/1.1. Combined with the -I command line parameter, we can use this Curl command to check if a website supports HTTP/2.
7. Do Follow Redirects
Curl command to follow redirects
curl -L http://www.reqbin.com/echo
By default, Curl does not follow redirects (HTTP status codes 301 and 302). The -L option tells Curl to do HTTP redirects.
The --user-agent command-line option allows you to pass any string to be used instead of the standard Curl User-Agent string. This can be useful if the server only expects requests from certain browsers.