What is cURL?
cURL, short for “Client for URLs”, is a command line tool for transferring data using various protocols. This tool has applications in many household products such as tablets, printers, cars, routers, etc.
There are a vast amount of use-cases for cURL such as:
- FTP upload,
- Proxy support,
- SSL connections,
- HTTP post, etc…
This tool also supports the use of all the following protocols: DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP.
This guide will outline a few popular cURL examples, along with a description of what each command does.
The following commands can all be entered directly into your terminal to retrieve a response.
1) HTTP GET Request
The first example is the most basic example which demonstrates a simple curl command that simulates a GET request for a website URL. This command will output the HTTP response of the URL in question.
2) Returning Only the HTTP Headers of a URL
-I option is used to tell cURL to only fetch the HTTP headers (HEAD method) of a particular page or resource.
curl -I https://www.keycdn.com/
3) Saving the Result of a cURL Command
-O cURL options are used to save the result of the cURL command. The difference between both options is that
-o will save the file with a predefined filename, which in this case is “myfile.css”. On the other hand, the
-O option will save the file as its exisiting name which is “animate.min.css”. An example of each scenario is shown below.
curl -o myfile.css https://cdn.keycdn.com/css/animate.min.css
curl -O https://cdn.keycdn.com/css/animate.min.css
4) Adding an Additional HTTP Header
This cURL command has the ability to add an additional HTTP Header to your GET requests. Simply use the
-H option and set the header name and value in enclosed quotes. If you do not define a value for the header then the header itself must be followed by a semi-colon (e.g. X-Header;).
curl -H "X-Header: Value" https://www.keycdn.com/
5) Generating Additional Information
-v option (for verbose) can be defined within a cURL command so that it will generate more information during the operation. For example, using the additional header example from above, we can add the
-v option which will display more information in regards to the connection and will display the custom header as well as regular headers.
curl -H "X-Header: Value" https://www.keycdn.com/ -v
6) Resuming a Download
If a download is started for a particular asset but gets interrupted or purposely stopped, it can easily be resumed with the -C option. Simply add
-C - to the cURL command in question and the asset will resume downloading where it left off.
curl -C - -O https://cdn.keycdn.com/img/cdn-stats.png
7) Storing HTTP Headers
-D option, you have the ability to store the HTTP headers that a site sends back. This is useful for instance if you want to read the cookies from the headers by using a second cURL command and including the
-b option. The
- after the
-D tells cURL that the output file is stdout (the file into which kernel writes its output).
curl -D - https://www.keycdn.com/
8) Testing the Download Time of an Asset Without Any Output
The following command uses a couple of cURL options to achieve the desired result. The
-D - tells cURL to store and display the headers in stdout and the
-o option tells cURL to download the defined resource. However, if you do not want any output, simply add
/dev/null to the end of the command. This example can be useful if you are testing the download speed of an asset but don’t want to print or save the output.
curl -D - https://www.keycdn.com/ -o /dev/null
9) Specifying a Maximum Transfer Rate
You can specify the maximum transfer rate for both uploads and downloads with the
--limit-rate option. The rate is measured in bytes/second unless a suffix such as “K” for kilobytes, “M” for megabytes, or “G” for gigabytes is added to the end of the specified number.
curl --limit-rate 200K -O https://cdn.keycdn.com/img/cdn-stats.png
10) HTTP/2 Support Check
If you have the latest cURL release, you can use the
--http2 option to check if a particular URL supports the new HTTP/2 protocol. Therefore, if the site does support HTTP/2, you will see HTTP/2.0 200 in the header instead of HTTP/1.1 200
curl -I --http2 https://cdn.keycdn.com/
11) Retrieving a Particular Byte Range
-r option to retrieve a particular byte range of a document. This essentially means to retrieve a particular portion of a file. These byte range commands can be specified in a number of ways (e.g. 0-499, 500-999, etc). Read our article to learn more about Byte Range Requests.
curl -r 0-20000 -o myfile.png https://cdn.keycdn.com/img/cdn-stats.png
12) cURL Usage Help
-h option to quickly retrieve a list of helpful command line options with associated descriptions.
cURL Examples to Simulate HTTP Methods
cURL can also be useful for testing HTTP methods. The following is a list of request methods that can be used by running a cURL command.
13) GET Method
The GET method is used to retrieve resources from a particular URL. The simple
curl https://www.keycdn.com/ command will use GET as the default HTTP method, however it can also be specified using
--request GET or
curl --request GET https://www.keycdn.com/
14) POST Method
The POST method is used to post information to a web server (e.g. a comment on a forum). This can be specified using
--request POST or
curl --request POST https://yourwebsite.com/
15) DELETE Method
The DELETE method deletes the resource from the web server associated with a specific URL. This can be specified using
--request DELETE or
curl --request DELETE https://yourwebsite.com/
16) PUT Method
The PUT method creates or replaces a resource based on the data the client submits to the web server. (e.g creating a new web page or updating an existing one). This can be specified using
--request PUT or
curl --request PUT https://yourwebsite.com/
17) Making cURL Requests With Data
You can make requests using certain HTTP methods and also send along data via the
--data option. The example below uses a POST request which sends login data to a login page of a particular website.
curl -X POST http://www.yourwebsite.com/login/ -d 'username=yourusername&password=yourpassword'
Popular cURL Examples – In Summary
The above cURL examples are amongst some of the most used and most popular. cURL is a very useful tool for a variety of purposes including debugging, development, etc. Use this cURL reference guide to help define specific cURL examples of your own and visit the cURL man page for a full list of cURL options.