Curl has made data transfer between two machines connected on the same network much easier. As curl command adheres to a specific URL syntax to successfully transfer data over a network.
By default, no user interaction is needed during the execution of the curl command. Out of the numerous protocols it supports, the prominent ones are HTTP, HTTPS, SFTP, SCP, SMTP, SMTPS, FTP, FTPS, and FILE.
This article will walk us through some essential curl-related commands that can be useful to developers seeking a breakthrough under a Linux operating system environment.
Install Curl Command in Linux
If you do not already have curl installed on your Linux distribution, reference the following installation guide for different distributions.
$ sudo apt install curl [On Debian, Ubuntu and Mint] $ sudo yum install curl [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a net-misc/curl [On Gentoo Linux] $ sudo pacman -S curl [On Arch Linux] $ sudo zypper install curl [On OpenSUSE]
Important Curl Commands Usage
We will categorize these important curl commands in the following manner:
Curl Command Help
These commands keep you better informed about the usage of curl.
$ curl --help
Provide quick assistance on the general usage of curl.
Expounds on the Curl options outputted by the curl –help command.
$ curl --help | grep [search_value]
Curl Command Manual
Gives users access to the curl manual page.
$ man curl
General Curl Command Usage
This section will cover the primary usage of curl commands with options and arguments.
Curl Show Web Page Content
The following command retrieves and prints a targeted URL’s content on standard output (stdout).
$ curl https://github.com/ranger/ranger/blob/master/README.md
Curl Write Output to File
Instead of printing on standard output (stdout), a targeted URL’s content is printed on a specified file.
$ curl URL > https://github.com/ranger/ranger/blob/master/README.md OR $ curl -o file2 https://github.com/ranger/ranger/blob/master/README.md
The following command writes output to a local file (takes the same name as the downloaded file).
$ curl -O URL
Curl Print Verbose Details
The following command will print verbose details linked to the URL target.
$ curl -v URL
Curl Trace Dump Data
The following command enables a full trace dump on all incoming and outgoing data.
$ curl --trace file_name URL
Curl Hide Error Messages
The following command will hide error messages and progress meter during data transfer (silent mode).
$ curl -s URL
Curl HTTP Headers Commands
This section of curl commands will help you set and read the HTTP Headers from the terminal.
Curl Fetch HTTP Headers
The following command will fetch the headers from the server (HEAD).
$ curl -I URL
Curl Add HTTP Header in Output
Associate curl command output with HTTP header.
$ curl -i URL
Curl Pass Multiple Headers to Server
Initiate server requests with multiple headers.
$ curl -H "HTTP General Header" -H "HTTP Request Header" URL
Curl Add Custom HTTP Header to Server
Initiate server requests with the custom HTTP header.
$ curl -H "{X-Key}:{Value}" {URL}
Curl Write Protocol Headers to File
Specify a file for writing received protocol headers.
$ curl -D file_name URL
Curl HTTP Cookies Commands
This section of curl commands will help you set and read the HTTP Cookies from the terminal.
Curl Save HTTP Cookies to File
All HTTP cookies are saved to a specified file.
$ curl -c file_name URL
Curl Read HTTP Cookies from File
The following command will read cookies from the file and pass data to HTTP Server in the Cookie header.
$ curl -b file_name URL
Curl Read HTTP Cookies from Data
The following command will pass the cookie header to the HTTP server from data (key: value).
$ curl -b "{key1:value1;key2:value2}" {URL}
Curl Delete HTTP Session Cookies
Destroy existing session cookies for a new session.
$ curl -j URL
Proxy Curl Commands Usage
This section of curl commands will help you set the proxy server URL and port number and also with authentication.
# Make use of a specific proxy server. $ curl -x {proxy_url}:{port} {URL} # Proxy authentication with username and password. $ curl -U {username}:{password} - x {proxy_url}:{port} {URL} # List/include non-proxy hosts $ curl --noproxy {url1} -x {proxy_url}:{port} {URL}
FTP Curl Commands Usage
This section of curl commands will help you to transfer files to and from an FTP Server.
# Anonymously print FTP site’s directory listing. $ curl FTP_URL # Anonymously downloada specific file fromthe FTP server. $ curl FTP_URL/file_name # Anonymously upload file to a server via FTP $ curl -T /path/to/file FTP_URL # Securely upload file to a server via FTP $ curl -u ftp_username:ftp_user_password -T "file_a,file_b" FTP_URL # Anonymously upload and append a file to an existing one. $ curl -T /path/to/file -a FTP_URL/existing_file
Curl HTTP Post/Put Data Commands
This section of curl commands will show you how to enclose data as part of a request.
# Initiate HTTP server POST/PUT request. $ curl -d 'key2=value2&key3=value3' -X POST/PUT URL # Initiate HTTP server POST/PUT request with data sent from a file. $ curl -d @file_name -X POST/PUT URL
Curl REST API Commands
This section of curl commands will illustrate how to invoke REST APIs with different HTTP methods.
# GET and JSON REST API invocation. $ curl -i -H "Content-Type: application/json" -X GET REST_API # POST and JSON REST API invocation. $ curl -i -H "Content-Type: application/json" -X POST -d 'key:value' REST_API # PUT and JSON REST API invocation. $ curl -i -H "Content-Type: application/json" -X PUT -d 'key:value' REST_API # DELETE REST API invocation. $ curl -X DELETE REST_API/ID
Curl Security Commands
This section of curl commands will show some of the ways to perform authentication.
# Authenticate a user with an associated password. $ curl -u username:user_password URL # SSL certificate plus password authentication via curl. $ curl --cert certificate_file.pem:password URL
Curl Configuration File
The curl configuration file .curlrc
should exist in the user home directory, if not, create it:
$ nano ~/.curlrc
Sample configuration entries associated with this file include:
# Timeout in seconds -m 100 # Custom default proxy setting proxy = 192.168.100.29:8080 # Proxy user and password setting proxy-user = "username:user_password"
With these Curl commands cheat sheet, Linux-based developers will have some confidence interacting with data over a network.
Credit: André Maré