Popular curl Examples
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
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.
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 request header
This curl command has the ability to add an additional HTTP request header to your 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 semicolon (e.g.
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
K for kilobytes,
M for megabytes, and
G 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
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.
500-999). 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.
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/
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/
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/
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'
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.