Linux Curl Command

cURL stands for client URL. This command is very popular in linux to transfer data to and from a server. It is a command line tool that can be used in Linux, Windows, and Mac O/S.

Using curl you can make rest api calls like:

  • GET
  • POST
  • PUT


The syntax for the curl command is as follows:

curl [options...] <url>

How do I send a GET request using Curl?

To send get request using curl you can use following syntax:

# basic get request

# get request with headers
   -H "Content-Type: application/json"

# fetch only headers using get request
curl -I

# curl does not follow redirects however you can tell
# curl to follow the redirects using -L option
curl -L

# you can send cookies to the server using the -b command-line
   -b "session=GHIJKLMNOPQRSTUV"

How do I send a POST request using Curl?

To send post request using curl you can use following code:

# send a post request with body
curl -X POST
   -H 'Content-Type: application/json; charset=UTF-8'
   -d '{title: 'foo', body: 'bar', userId: 1}'

# if you want to send json file as data in your curl you can use
curl -X POST
   -H 'Content-Type: application/json; charset=UTF-8' 
   -d @post.json

# curl post with file
curl -d @path/to/data.json

How to allow insecure HTTPS connections using Curl?

When sending https request curl tries to verify the certificate of the target url against the local CA certificate store. To bypass ssl check use -k option as shown below:

# disable ssl certificate checks
curl -k
curl --insecure

# send client certificate to verify ssl
curl -E cerfile.crt
curl --cert cerfile.crt

# send ca certificate
curl --cacert ca_cert.cert

How do I download a file using Curl?

To download a file with Curl, use the --output or -o command-line option.

curl -o filename.txt

How do I send Basic Auth Credentials with Curl?

Sometime urls are protected with authentication to send basic auth credentials along with your curl request you can use following -u flag:

# send basic auth
curl -u "username:password"

# alternatively you can base64 your credentials and use like this
   -H "Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ="

How do I use Curl with a proxy?

Sometime you may want to use proxy to access remote url. To use a proxy with Curl, you must pass the required proxy address using the -x (or --proxy) command-line option.

# syntax
curl -x "[protocol://][host][:port]" [URL] [options]

# example
curl ttps:// -x -U login:password

How to use bearer token in curl?

Sometimes your server allows you to use bearer token for authentication purpose to use bearer token in curl:

# send bearer token as authorization header
   -H "Accept: application/json"
   -H "Authorization: Bearer {token}"

# send a post request with body
curl -X POST
   -H 'Content-Type: application/json; charset=UTF-8'
   -H "Authorization: Bearer {token}"
   -d '{title: 'foo', body: 'bar', userId: 1}'

How do I send a HEAD request using Curl?

Curl -I option tells curl to only send and HTTP HEAD request to receive only HTTP headers. HEAD request is very similar to GET request however it only returns headers without response body.

# example - 1
curl -I

# example - 2
curl --head

# example -3 
curl -X HEAD

How do I send a DELETE request using Curl?

To make a DELETE request using Curl, you need to use the -X DELETE command-line option:

# syntax
curl -X DELETE [URL] [options]

# send delete request using curl
curl -X DELETE -H "Accept: application/json"

Curl command options table

Flag Description Usage
-O Download the file and save as original name curl -O [URL]
-o Download file and save as diff name curl -o [file_name] [URL]
-X Specify http method like i.e. GET,POST,PUT,DELETE,OPTIONS curl -X [method] [URL]
-I Return only headers curl -I [URL]
-k Ignore SSL verification curl -k [URL]
-u send auth request curl -u [user:password] [URL]
-H send http headers curl -H "X-Header: value" [URL]
-v make verbose curl -v [URL]
-L follow redirects curl -L [URL]
-x send proxy request curl -x "[protocol://][host][:port]" [URL] [options]
--cookie send cookie along with request curl --cookie "Name=Value" [URL]