How to Make API Request From Command Line with CURL

April 8, 2020

Why learn it?

cURL (Client URL) is a powerful command line tool for working with URLs.

If you want to test API outputs for your programs, you can use softwares like Advanced REST Client, POSTMAN, or Postwoman. But if you want to do something quick, universal (easily installed in most OS), and also as powerful, you can use cURL. It takes a little more learning, but it is worth it.

Fun fact: someone suggested to revise the abbreviation to use recursive acronym.

"Curl Url Request Library" (CURL)

Which, when expanded, becomes:

Curl Url Request Library Url Request Library -> 
Curl Url Request Library Url Request Library Url Request Library ->
Curl Url Request Library Url Request Library Url Request Library Url Request Library ->
... etc

cURL can be used with many protocols:


I will cover only one here, HTTPS, because I think that will be the most useful ones for testing APIs (another useful one to learn is FTP). This article is a basic introduction to cURL. If you wish to learn more, check out resources.

I will use jsonplaceholder site throughout this article. Let's get started!

Basic usage - cURL with GET

The basic syntax is:

curl your-url

For example:


cURL will return web page response.

cURL with query params

Sometimes we need to use query params to filter our search. There is no additional option that we need to pass. Recall that query string params have the following syntax:


For example:


cURL with header information

If you want to see header information about your request, add -i

curl -i

It will add something like:

HTTP/2 200
date: Tue, 07 Apr 2020 12:47:16 GMT
content-type: application/json; charset=utf-8
set-cookie: __cfduid=d995d97cc8c7b809dbd5a490b2db28e2f1586263636; expires=Thu, 07-May-20 12:47:16 GMT; path=/;; 
server: cloudflare
cf-ray: 5803db316c66f19e-ATL

// Endpoint response here

cURL + downloading file

You can download file with -O option. On today's google main page, I see this cool gif and I want to download it.

Google main page screenshot on April 8th 2020

Once you locate the URL with inspect, you can download it with:

curl -O

It downloads to your current directory where you run that command.

If you want to rename your file, you can use -o. The syntax is curl -o new-file-name file-location-url
. For example:

curl -o iggy-google-image.gif

You don't have to save it to current location. You can change its directory and file, like:

curl -o ~/Images/iggy-google-image.gif

cURL with POST

By default cURL runs a GET request. To use different HTTPS method, you can use -X (--request). To do POST request with cURL, the syntax is:

curl -X POST my-url

Btw, our GET cURL earlier:


The code above is technically the same as -X GET, because if we don't explicitly specify, it will default to GET:

curl -X GET

Sending data (-d)

We probably can't do much with POST if we just do curl -X POST my-url. We need to pass it some data in our request body. To pass data in cURL, use -d. For example:

curl -X POST -d 'title=test title&body=no body'

If you notice, the format of our body is similar to query params: key=value&key2=value2&...

To pass JSON type data in body:

curl -X POST -d '{"title": "hello json"}'

Adding header

When we are sending data, we may need to include header. For example, to notify the server that we are sending JSON, we add "Content-type: application/json". We can use -H.

This is what our request looks like with header added:

curl -X POST -d '{"title": "hello json"}' -H "Content-type: application/json"

You can add multiple headers with cURL by having multiple -H in one request:

curl -X POST -d '{"title": "hello json"}' -H "Content-type: application/json" -H "Accept-Charset: utf-8" -H "Accept-Language: en-US"

cURL with PUT

Just like POST, we can do PUT request:

curl -X PUT -d '{"title": "hello put"}'


Curl also works with PATCH:

curl -X PATCH -d '{"title": "hello patch"}'


Curl works with DELETE:

curl -X DELETE


This is a good place to stop. This article provides a guide how to get started with cURL. There are many more features I did not cover. I would suggest checking out the resources below to learn more.

Thanks for reading. Happy coding!

# Resources - [How to start using Curl and why: a hands-on introduction]( - [Using curl to automate HTTP jobs]( - [Everything curl]( - [cURL manpage](
© Copyright 2021 Igor Irianto