cURL - Client URL

Client URL (cURL) is a command-line tool for transferring data from or to a server. It primarily supports http along with many other protocols such as HTTPS, IMAP, GOPHER, SCP, SMB, etc. Unlike web browsers, cURL does not render the HTML/JavaScript/CSS code. It rather prints the code in its raw format.

There are tons of options provided by cURL that allows us to customize our requests by modifying different headers and data that it may contain. All the different options available can be a little overwhelming at the beginning. cURL utilizes libcurl for all transfer-related features.

How To Find Files In Linux Using The CLI

Syntax

The basic syntax of using the curl command is:

curl [options/URLs]
  • The options start with one or two dashes and many of the options require an additional value next to them.
  • The URLs describe the location of the resources that need to be accessed.

Examples

Let's look at some of the examples.

1. When you use curl without any optioins, it sends a GET request to the specified URL.

curl https://archlinux.org
  • This command will send a GET request to https://archlinux.org and then display the raw output of the response in the terminal.

2. The response can be saved to a file using -o and -O options.

curl -o response.html https://archlinux.org
curl -O https://archlinux.org
  • The -o option can be used to specify the filename to save the response to. In the above command, the response will be saved to response.html file.
  • The -O option uses filename that is present in the remote server to save the response.

3. The use of -v option allows us to print the verbose output.

curl -v https://archlinux.org
  • The -v option prints both the request and response.
  • We can increase the level of verbosity by using -vvv.

4. The use of -I option allows us to send a HEAD request.

curl -I https://archlinux.org
  • The above command would print only the response headers.
  • The -i option can be used if we want to view response headers as well as the response body.

5. We can use -A option to set the User-Agent in the request header.

curl -A 'Mozilla/5.0' https://archlinux.org
  • Using -H option, we can set other headers as well. For example:
    • -H 'Authentication: Basic Wdboeahr83al2sce='
    • -H 'content-type: text/html; charset=utf-8'

6. The -X option allows us to send a POST request and we can use -d option to include data with the POST request.

curl -X -d 'username=admin&password=admin' https://somesite.com/login
  • -X PUT allows us to send PUT request.
  • -X DELETE allows us to send DELETE request.
  • We can use -L option if we want to follow the redirection after submitting the POST request.

7. We can use -b option to set the cookie header. This can also be done by using -H option.

curl -b 'PHPSESSID=fjme928ann38dhaje2ndandf93qne902' https://somesite.com/dashboard
curl -H 'Cookie: PHPSESSID=fjme928ann38dhaje2ndandf93qne902' https://somesite.com/dashboard