“cURL” to transfer data in URL syntax
“cURL” is a powerful command line tool and library for transferring data with URLs
(since 1998) – quoted in https://curl.se , we commonly use it to send the HTTP GET and POST requests especially in terminal mode. We will have some basic usage to share here.
Get the http response code
It’s very common to use “curl” to collect the http response code in shell environment, e.g. you may need to verify if remote URLs are working fine (http code = 200).
# the "\n" means to print the newline at the end of http_code
# "-s" and "-o /dev/null" just to omit the length output
$ curl -w '%{http_code}\n' -s -o /dev/null "https://google.com"
301
$ curl -w '%{http_code}\n' -s -o /dev/null "https://www.yahoo.com"
200
Collect the response time
As you can see in previous example, we use %{http_code}
to get the http response code, we have other parameters to be used when we want to collect the latency, e.g.
$ curl -s -o /dev/null -w '\nEstablish Connection: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n' "https://www.yahoo.com"
Establish Connection: 0.010960s
TTFB: 0.244146s
Total: 0.883493s
You can also to create a TEXT file, e.g. curl.txt and then let “curl” to use it to get formatted output.
$ cat curl.txt
time_namelookup: %{time_namelookup}s\n
TCP_Connect: %{time_connect}\n
SSL_Connect: %{time_appconnect}\n
time_connect: %{time_connect}s\n
time_appconnect: %{time_appconnect}s\n
time_pretransfer: %{time_pretransfer}s\n
time_redirect: %{time_redirect}s\n
time_starttransfer: %{time_starttransfer}s\n
----------\n
time_total: %{time_total}s\n
# and then run the "curl" command
$ curl -w '@curl.txt' -s -o /dev/null "https://www.yahoo.com"
time_namelookup: 0.009075s
TCP_Connect: 0.012072
SSL_Connect: 0.042414
time_connect: 0.012072s
time_appconnect: 0.042414s
time_pretransfer: 0.042584s
time_redirect: 0.000000s
time_starttransfer: 0.242770s
----------
time_total: 0.886892s
Check “curl” manual page to see what other available variables you can use. That’s a lot..:)
Send multiple requests in one shot
You can repeat the commands by 5 times, or you can try to use :
$ curl -w '%{http_code}\n' -s -o /dev/null "https://search.yahoo.com/search?p=apple"
# add "?n=[1-5]" in the end of curl command:
$ curl -w '%{http_code}\n' -s -o /dev/null "https://search.yahoo.com/search?p=apple?n=[1-5]"
200
200
200
200
200
x
Use “curl –resolve” to change the actual destination
This is useful when you want to test the original in a particular IP address, let’s say you have 5 hosts behind : www.yahoo.com and you can use the following sample to connect to particular one :
curl --resolve www.yahoo.com:443:10.10.10.1 https://www.yahoo.com/
Get the size of response header and body
As we said, there’re lots of variables available in curl to be use, and “size_header” and “size_download” can be used in this case.
$ curl -s -o /dev/null -w 'Header: %{size_header}\nBody: %{size_download}\n' "https://www.google.com/search?q=apple"
Header: 1100
Body: 139165