Curl examples

curl is a CLI tool that allows you to transfer data to or from a server in a variety of different protocols. In this post, you can find some examples that demonstrate the basic curl options for the HTTP/HTTPS protocols.

Table of contents

Make a GET request

curl https://www.google.com

You don’t have to use an option because GET is the default HTTP method.

Specify the HTTP Method

You can specify the HTTP Method with the -X or --request option. In the following example, I make an OPTIONS request:

curl -X OPTIONS -i https://jsonplaceholder.typicode.com/

The -i or --include option gives back the response headers, not only the response body; more about that in a bit.

1. Perform a POST request

When you perform a POST request, you usually send some data to the server. You can send data with the -d or --data option:

curl --data "title=Groceries&body=Buy groceries." https://jsonplaceholder.typicode.com/todos

You don’t have to state the method with -X POST because POST is the default method when you use the --data option. You send the data as content-type: application/x-www-form-urlencoded. See also -F or --form and --data-raw.

2. Update with the PUT method

If you want to perform a PUT request, you have to specify the method along with the data you want to send:

curl -X PUT --data "title=Groceries&body=Buy groceries." https://jsonplaceholder.typicode.com/todos/1

Get the headers

When you make an HTTP request with curl, the default output you get is the response body. If you want to get back the headers too, you have 3 options:

1. Get both response headers and body

This is what you want most of the times. To get both response headers and body, you can use the -i or --include option:

curl https://www.google.com/ -i

2. Get only the response headers

To get only the response headers—without the response body—use the -I or --head option:

curl https://www.google.com/ -I

With this option, you perform a HEAD HTTP request. You can achieve the same thing if you set the HTTP method to HEAD with curl -X HEAD. But because you get only the response body by default, you also have to include the headers with -i or --include, so the final command would be: curl -X HEAD -i. As a result, the -I or --head option is preferred.

3. Get the request headers too

Sometimes, you want more details about the exchange between the server and the client. For example, you may want to see the request headers. In this case, you can use the -v or --verbose option. Be careful, though, you get a lot more information, not only the request headers:

curl https://www.google.com/ --verbose

To find the request headers, look for the lines that start with >. The response headers, on the other hand, start with <. The lines that start with * are TCP/TLS stuff.

Follow redirects

To follow the redirects use the -L or --location option. You should also append the response headers with the -i option to see what’s going on:

curl http://google.com --location -i

Try the same request without the --location option if you want to see the difference.

Save console output to file

You can use the -o or --output option to save the console output in a file:

curl https://www.google.com -i -o google.txt

Save a file

If you want to save a file—keeping its original filename—in your local machine, use the -O or --remote-name option:

curl -O https://markoskon.com/static/fa71d793057a033cf6b13329754cb6fc/dbbca/all-cool-dev.jpg

If you want to name the file, use the -o or --output instead:

curl -o gatsby-modals.jpg https://markoskon.com/static/fa71d793057a033cf6b13329754cb6fc/dbbca/all-cool-dev.jpg

Advanced

More useful options for another post:

Further reading

Other things to read

Popular posts

Other notes