cURL tutorial for beginners

In this tutorial we will learn about cURL and also find out different use cases where we can use this command line tool.

Introduction

cURL is a command line tool for transferring data with URLs. It supports FTP, HTTP, HTTPS, proxy tunneling and many other protocols.

Tool Name cURL
Developed By Daneil Stenberg
First release date March 20, 1998

Printing response headers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
curl -sSL -D - https://www.google.com -o /dev/null


HTTP/2 200 
date: Thu, 09 May 2019 14:47:17 GMT
expires: -1
cache-control: private, max-age=0
content-type: text/html; charset=ISO-8859-1
p3p: CP="This is not a P3P policy! See g.co/p3phelp for more info."
server: gws
x-xss-protection: 0
x-frame-options: SAMEORIGIN
set-cookie: 1P_JAR=2019-05-09-14; expires=Sat, 08-Jun-2019 14:47:17 GMT; path=/; domain=.google.com
set-cookie: NID=183=Kx7qT7AShnSFmdfmzp2HSJ1L_pXzVO8fNwUb7Ps7edudbRyrGivsIDpw688ut01484SfwrKfMVm74ryDmwXEhzqkeL89_uMh0W-XVFyAGW2wnC_GKxPZF-y69v-bvbDJKKJ_Qa6vs3Ll2KJiLCS_t6UE1okYMsVNE7jaT0PAD_s; expires=Fri, 08-Nov-2019 14:47:17 GMT; path=/; domain=.google.com; HttpOnly
alt-svc: quic=":443"; ma=2592000; v="46,44,43,39"
accept-ranges: none
vary: Accept-Encoding

Downloading a file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# specifying file name
curl -o setup.sh https://raw.githubusercontent.com/getredash/redash/master/setup/setup.sh

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2941  100  2941    0     0   7419      0 --:--:-- --:--:-- --:--:--  7408

# keeping the file name same
curl -O https://raw.githubusercontent.com/getredash/redash/master/setup/setup.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2941  100  2941    0     0  19477      0 --:--:-- --:--:-- --:--:-- 19606

Sending header using a HTTP request

1
curl -H "Content-Type: application/json" www.google.com

Measuring request and response time

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
create file perf.txt
----
    time_namelookup:  %{time_namelookup}\n
       time_connect:  %{time_connect}\n
    time_appconnect:  %{time_appconnect}\n
   time_pretransfer:  %{time_pretransfer}\n
      time_redirect:  %{time_redirect}\n
 time_starttransfer:  %{time_starttransfer}\n
                    ----------\n
         time_total:  %{time_total}\n
-----         

curl -w "@perf.txt" -o /dev/null -s https://google.com/

    time_namelookup:  0.014838
       time_connect:  0.021859
    time_appconnect:  0.136142
   time_pretransfer:  0.136409
      time_redirect:  0.000000
 time_starttransfer:  0.239219
                    ----------
         time_total:  0.240644

Get status code

1
2
3
4
5
curl -I https://google.com 2>/dev/null | head -n 1 | cut -d$' ' -f2
301

curl -I https://wordpress.com/ 2>/dev/null | head -n 1 | cut -d$' ' -f2
200

Posting json data to API endpoint

1
2
3
4
5
curl -X POST https://api.mathpix.com/v3/latex \
    -H 'app_id: trial' \
    -H 'app_key: 34f1a4cea0eaca8540c95908b4dc84ab' \
    -H 'Content-Type: application/json' \
    --data '{ "src": "data:image/jpeg;base64,'$(base64 -i limit.jpg)'" }'

Overriding DNS lookup for a specific hostname/port combination

No need to manually update /etc/hosts file. This option overrides DNS lookup on the fly.

1
curl --resolve example.com:80:127.0.0.1 http://example.com