cURL cheat sheet

cURL cheat sheet

Basic usagePermalink

curl url

The above will make a GET request to the URL and display a response body.

View response headersPermalink

We can use the - head option to see only response headers.

curl --head url

If we want to see response headers AND response body, we can use the -i option:

curl -i url

Save response to a filePermalink

By default, curl outputs everything to the screen (terminal).

To redirect its output to a file, we can use options -o or -O.

-o option lets us tell curl into which file it should write output to.

curl url -o file.txt

Please note that whatever would be printed to the screen will now be saved to the file — including response headers if we used the -i option curl -i url -o file.txt.

If we don't need to specify a custom file name, we can use the option -O and curl will save its output to the file with the same name as the file name in the url.

curl -O url

Download filePermalink

We can use the -O or -ooption to download a file. But we need to make sure we don't use other options that modify the output like -i.

curl -O url_of_the_file 

If we want to save a file with a different name than it has on the server:

curl url_of_the_file -o desired-name

Examples:

curl -O https://michaeliscoding.com/images/0029-curl-cheat-sheet.jpg
curl https://michaeliscoding.com/images/0029-curl-cheat-sheet.jpg -o curl.jpg

Send headersPermalink

To send headers, we use the -H option like this

curl -H "Header-name: header-value" url

If we want to send multiple headers, we provide -H options multiple times:

curl -H "Header-name: header-value" -H "Other-Header: -other-header-value" url

Examples:

curl -H "Content-Type: application/json" url
curl -H "Content-Type: application/json" -H "Accept-Encoding: gzip, deflate" url

HTTP MethodPermalink

Unless specified otherwise, curl will make a request using the GET method.

To change the HTTP method, use the -X option.

curl -X METHOD url

Examples:

curl -X POST

Send dataPermalink

When making POST or PUT requests, we need to send data with a request.

To send data, we use the -d option.

curl -d 'our data here' url

Example:

curl -X POST -d '{"id":1, "name": "John"}' url

When sending data, don't forget to specify the Content-type header using the -H option.

Send data from filePermalink

If the data we want to send in the request body are in the file, e.g. we need to use the -d option and then use the @ sign before the name of the file like this:

curl -d @my_file.json url

Follow redirectsPermalink

Sometimes response from the server has a 301 or 302 status code which means redirect to another page. Curl will not follow the new URL by default, but we can tell him to do so with the -L option.

curl -L url

More info about request and responsePermalink

To get more information about request and response, we can use the -v option — as a shortcut for verbose:

curl -v url

You might also like

Join the newsletter

Subscribe to get new articles about Python, code and programming into your inbox!