How to Use Curl

Updated on March 30, 2022
How to Use Curl header image


Curl is a software project consisting of the libcurl library and the curl command-line tool that supports FTP, FTPS, HTTP, HTTPS, and many other network protocols. Curl is pre-installed in many operating systems, and developers use it extensively to transfer data and make API requests. This guide explains how to use curl and make requests with the tool.


Using Curl

The basic curl syntax is:

curl [options] [url]

To download a webpage with curl:

$ curl

This downloads the complete webpage and displays the source code to your terminal. If you omit the protocol, curl defaults to https://.

If you want to use FTP instead:

$ curl

To save the contents of your download to a file, add the `-o' option followed by the filename:

$ curl -o filename.txt

This downloads the webpage to filename.txt. You can also omit the filename to take the default name from the server:

$ curl -O

If something interrupts your download, you can resume downloads with the -C option:

$ curl -C -O

Enable verbose mode with the short option -v or long option --verbose:

$ curl -v

Making HTTP Requests

Curl has more than 200 options for HTTP requests, a few of which include:

Getting Only Response Headers

To get only the headers from a response excluding the body, use the -I option:

$ curl -I

Following A Redirect

Curl doesn't follow redirects unless you add the -L option:

$ curl -L

Changing HTTP Method

By default, curl uses the GET method. To change methods, use the -X option followed by the method you want to use. For example, to use the DELETE method:

$ curl -X DELETE

Adding Extra Headers

To add extra headers, such as setting the User-Agent or Cache-Control field, use the -H option:

$ curl -H "User-Agent: Mozilla/5.0 (Macintosh)" -H "Cache-Control: no-cache"

Change the user-agent

You can change the user-agent with the --user-agent option:

$ curl --user-agent "Mozilla/5.0 (Macintosh)" -H "Cache-Control: no-cache"

Post Data

To send data with a POST request, use the `-d' option followed by the data you want to send:

$ curl -d "option1=value1&option2=value2" -X POST

The Content-Type is automatically set to "application/x-www-form-urlencoded" in the example above. To send JSON data and explicitly set the Content-Type to application/json, use the -H option to set the Content-Type:

$ curl -X POST -H "Content-Type: application/json" -d '{"option1":"value1", "option2":"value2"}'

Use Basic Authentication Credentials

If a resource requires basic HTTP authentication, pass the credentials with the -u option:

$ curl -u login:password

Curl automatically converts the login:password pair to a base64-encoded string token and adds the Authorization: Basic <base64 token> header to the request.

More Information

This guide doesn't cover every curl option. These are only the most popular uses of the tool. For more information, check the man page.