Linux Curl Command Detailed Tutorial

curl is a command-line tool for transferring data between a local computer and a remote server

9 min read
By myfreax
Linux Curl Command Detailed Tutorial
Linux Curl Command Detailed Tutorial

Curl is a command-line tool for transferring data between a local computer and a remote server.

When using curl you can download or upload data using protocols such as HTTP, HTTPS, SCP , SFTP and FTP .

Curl provides many options that allow you to resume uploads/downloads, limit bandwidth, proxy support, user authentication, and more.

In this tutorial, we will explain how to use Curl command to download files in Linux and the detailed explanation of its options. If you don't specify a protocol to use, Curl defaults to using the HTTP protocol.

The Curl command is pre-installed on most Linux distributions. To check if your Linux distribution has Curl installed, press the shortcut key CTRL+ALT+T to open Terminal, type curl, and press Enter.

Install Curl

If curl is installed, the system will print curl: try 'curl --help' or 'curl --manual' for more information.

Otherwise, the terminal prints the message curl command not found . If you don't have Curl installed , you can install it using your distribution's package manager.

If your computer is running a Debian-based Linux distribution such as Ubuntu, Linux Mint, etc. Please run sudo apt install curl the command to install curl.

If your computer is running a Redhat-based Linux distribution, such as CentOS, Fedora, etc. Please run sudo yum install curl the command to install Curl.

sudo yum install curl
sudo apt install curl

Curl Commands and Options

curl The grammatical form of the command is curl [options] [URL...], options Curl's options , optional parameters. URL Indicates the address of the remote server.

When the Curl command is run without specifying any options, the curl command downloads the specified URL resource and redirects it to standard output .

For example, the command curl https://www.google.com/ downloads the Google homepage and prints the source code of the Google homepage on the terminal.

Send JSON Body

When sending the JSON Body to the server, you need to set the header Content-Type to application/json instruct the Curl command to send the Body data in JSON format.

In addition to setting the header , you also need to use the -d / -data option of the Curl command to specify the JSON string to be sent. Note that JSON needs to be escaped with single quotes.

For example the command curl -X POST -H "Content-Type: application/json"  -d 'JSON String' URLsends  a JSON string {"email":"web@myfreax.com"} to the server.

curl -X POST -H "Content-Type: application/json"  -d '{"email":"web@myfreax.com","website":"myfreax.com"}' http://127.0.0.1:3000/site

POST JSON and upload the file

When sending JSON data and uploading files to the server, you need to set the Content-Type of the header to multipart/mixed to instruct the Curl command to send data in a mixed format.

In addition to setting the Content-Type of the header, sending JSON format data is to specify the JSON string to be sent with the -F option of the Curl command. Note that JSON needs to be escaped with single quotes.

So the final form of the command is curl -X POST -H "Content-Type: multipart/mixed" -F blob=@file path -F 'metadata=JSON string;type=application/json' .

curl -i -X POST -H "Content-Type: multipart/mixed" -F "blob=@/Users/username/Documents/bio.jpg" -F "metadata={\"edipi\":123456789};type=application/json" http://localhost:8080/api/v1/user/

Upload files only

To upload a file using the Curl command, just specify the multipart MIME data in the key-value pair name=content using the -F option of the Curl command.

But when specifying a file with the -F option, @ symbols need to be added before the file path . Files can be any type of file including images, documents, etc.

For example, the parameter -F 'image=@/wallpaper.jpg' specifies that the Key is image, and the file is /wallpaper.jpg.

curl -X POST -F 'image=@/home/user/Pictures/wallpaper.jpg' http://example.com/upload

HTML form multipart/form-data

If you need to use the Curl command to simulate a form request, you can use the Curl command -F option to create a POST request and multipart/form-data encode it with.

-F Options allow you to specify multipart data. The Curl command automatically sets the content type to multipart/form-data send data.

You can also use -Foptions to specify multiple data, and the form of the data is specified in the form of field=valuekey-value pairs.

For example, the following Curl command will simulate an HTML form to send data in two fields, the field website value is myfreax.com, and the field email value is web@myfreax.com. Finally, specify the requested URL.

curl -X POST -F 'website=myfreax.com' -F 'email=web@myfreax.com' https://wwww.myfreax.com/contact.php

HTML form x-www-form-urlencoded

Another way to make a POST request is to use the -d options of the Curl command. -d The Curl command uses application/x-www-form-urlencoded an encoding of the data when using options.

Also specifies the content type as application/x-www-form-urlencoded. Requests sent in this way are sending data using query strings.

If you need to send a lot of data you don't have to use -d options multiple times, you can use & symbols to merge data, such as website=myfreax&email=myfreax@example.com.

curl -X POST -d 'name=myfreax' -d 'email=myfreax@example.com' https://example.com/contact.php

#eq
curl -X POST -d 'name=myfreax&email=myfreax@example.com' https://example.com/contact.php

Send Header

If you want to send the Header to the server, you can use the -H / --header option of the Curl command , which allows specifying the Key and Value of the Header.

A space must be used between the Key and Value of the header, and the header is wrapped in double quotes to avoid shell interpretation.

You can use multiple -H/ --header options at the same time to specify the Key and Value of multiple Headers. You can see that the following command will send multiple headers.

The first header sets the content type Content-Type: application/json and the second header is sent website: myfreax.com.

curl -X POST -H "Content-Type: application/json" -H "website: myfreax.com"  -d '{"email":"web@myfreax.com","website":"myfreax.com"}' http://127.0.0.1:3000/site

Download file

By default the curlcommand downloads the resource specified by the URL and redirects standard output .

If you want to save the download to a file, you can use the -o / -O option of Curl command's.

When using the lowercase -o option, you can specify the name of the saved file, or you can specify an absolute path, and the Curl command will download the file to the absolute path you specify.

The uppercase -O option of the Curl command will save the file with its original filename to the current directory, which is the directory from which the command was run.

For example, the command curl -O URL/vue.js downloads the vue.js file to the current directory.

The command curl -o /home/myfreax/vue3.js URL/dist/vue.js downloads the file vue.js and saves it to a file /home/myfreax/vue3.js.

After the download is complete, you can run the ls command to list the files downloaded by curl. ls vue* Indicates to list files whose names contain vue strings.

curl -o /home/myfreax/work/vue.js https://cdn.jsdelivr.net/npm/vue/dist/vue.js

curl -O https://cdn.jsdelivr.net/npm/vue/dist/vue.js

ls vue*

Download multiple files

To download multiple files at once, use multiple -O options or lowercase -o options followed by the URL of the file to download.

Same as downloading a single file with the command . When using lowercase -o options you need to specify the path to the download directory. Using the uppercase -O option will save the current directory.

You can also mix lowercase -o and uppercase -O options. The following curlcommand downloads the vue.js file to the current directory when the -O option is used, saving the file with a name vue.js.

The lowercase -o option downloads the wrok directory of the vue.js file to myfreax user's home directory, and saves the file as vue.js.

curl -o /home/myfreax/work/vue.js https://cdn.jsdelivr.net/npm/vue/dist/vue.js \
-O https://cdn.jsdelivr.net/npm/vue/dist/vue.js

ls /home/myfreax/work/vue.js && ls vue.js

Resume download

If you get disconnected during a large file download. Instead of restarting the download, an -C - option to the Curl command can be used to resume the previous download.

You can also specify the -C - option directly when downloading the file for the first time. When the download of a large file is disconnected, you can resume the download directly by using the previous command.

But one thing worth noting is that the resume download of the Curl command requires the server to support resume download, otherwise Curl will start downloading the file again.

curl -C - -O https://releases.ubuntu.com/22.04/ubuntu-22.04-desktop-amd64.iso

View HTTP response header

HTTP response headers are colon-separated key/value pairs that contain information such as server type, usually Nginx/Apache, content type, and HTTP status code.

The -I option allows you to view the HTTP response headers of the specified URL resource, as well as custom response headers set in Nginx or Apache.

For example, the command curl -I https://en.myfreax.com/ prints the URL https://www.ubuntu.com/ response header.

curl -I --http2 https://www.ubuntu.com/

Test HTTP/2 support

To check whether a specified URL supports the HTTP/2 protocol, use the Curl command with the -I and --http2 options to view the HTTP response headers, then use the grep command to search the standard output .

If you're using Curl version 7.47.0 or later, you don't need to use the --http2 option . By default, Curl uses the HTTP/2 protocol for connections by default.

The -s option runs Curl silently and hides progress and error messages. If the remote server supports the HTTP/2 protocol, the terminal prints the message HTTP/2.0 200, otherwise it does HTTP/1.1 200.

curl -I --http2 -s https://en.myfreax.com/ | grep HTTP/2
HTTP/2 200

301 HTTP redirect

If you try to scrape the google.com homepage without www the , you'll notice that Google returns the content of the 301 page, run the command curl google.com.

As you can see from the page content of the 301 redirect, it google.com is redirected to the www version. And by default Curl commands don't follow the HTTP Location header.

So you won't get the source code of the Google homepage. To follow a 301 redirect use the -L/ --location option of the Curl command, the Curl command will not terminate until the server does not return a status code of 301.

curl -L google.com

Specify User Agent User-Agent

You should encounter the situation that when using different browsers, some browsers can download, or some browsers cannot download. Or provide different page content according to the visitor's device and browser.

This is because the server program distinguishes the user's device type according to the User-Agent, so as to determine whether to return different content or prevent your download and access.

To prevent the server from serving different content based on the visitor's User-Agent. Use curl the command's -Aoption to impersonate the User-Agent user agent.

curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" https://getfedora.org/

Limit download speed

--limit-rate options of Curl to allow you to limit the data transfer speed/rate. The default speed unit is bytes. You can also use k, m, gas a suffix to indicate the download speed in different units.

The Curl command will download the Go binary and limit the download speed to 1MB, the -O option says to save to the file in the current directory.

curl --limit-rate 1m -O https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz

Download/upload FTP files

In addition to the HTTP protocol, the Curl command also supports FTP protocol file download and upload, and lists files on the FTP server.

If you need to access a protected FTP server using the curl command, use the -uoptions of the Curl command and specify the username and password.

The -T options of the Curl command allow you to upload files to an FTP server, followed by the file you want to upload, and you can also specify the absolute path of the file.

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/ #list file

curl -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/file.tar.gz #download file

curl -T newfile.tar.gz -u FTP_USERNAME:FTP_PASSWORD ftp://ftp.example.com/ #upload file

Send cookies

When using curl to make HTTP requests, by default, the Curl command does not send or store any cookie information.

Sometimes you may need to specify a cookie to initiate an HTTP request to access server resources.

The -b option of the Curl command allows you to send the specified cookie information to the server, please follow the -b option with the cookie string or the file name containing the cookie.

For example, if you want to download the rpm file of Oracle Java JDK, you need to send Cookie key-value pairs oraclelicense=a.

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/10.0.2+13/19aef61b38124481863b1413dce1855f/jdk-10.0.2_linux-x64_bin.rpm

use a proxy server

The Curl command supports different types of proxies, including HTTP, HTTPS, and SOCKS.

To transfer data through a specified proxy service, use the Curl command -x / --proxy option followed by the proxy server URL.

If the proxy server requires authentication, use the -U / --proxy-user option of the Curl command followed by the username and password separated by a colon.

Example command curl -x 192.168.44.1:8888 URL to use a proxy server 192.168.44.1:8888.

curl -U username:password -x URL specify the proxy server username and password.

curl -x 192.168.44.1:8888 http://linux.com/

curl -U mark:Passvv0rd -x 192.168.44.1:8888 http://linux.com/

Conclusion

So far, this tutorial has used very simple examples, but demonstrates the most commonly used Curl options, to help you understand how curlthe commands work.

Related Articles