18 things We Can Do with curl

Here is how to install curl on macOS/Windows/Linux
https://help.ubidots.com/en/articles/2165289-learn-how-to-install-run-curl-on-windows-macosx-linux

get ipinfo

We can get an IP address with curl command easily.

$ curl ipinfo.io
{
"ip": "xxx.xxx.xxx.xxx",
"hostname": "cpe-xxx-xxx-xxx-xxx.xxs.sss.sss.com",
"city": "Brooklyn",
"region": "New York",
"country": "US",
"loc": "xx.aaaa,yy.bbbb",
"org": "TTTTT Inc",
"postal": "xxxxx",
"timezone": "America/New_York",
"readme": "https://ipinfo.io/missingauth"
}%

generate QR code

The following is a QR code to access my personal website.
/ you can put anything you want to show(string, number, and url).

$ curl qrenco.de/https://kojikoji.ga
█████████████████████████████████
█████████████████████████████████
████ ▄▄▄▄▄ ██▀▄███ ██ ▄▄▄▄▄ ████
████ █ █ █▄▀█▄▀█▀▄██ █ █ ████
████ █▄▄▄█ ██▄▀▀ ▀▄ █ █▄▄▄█ ████
████▄▄▄▄▄▄▄█ █▄▀▄▀ ▀▄█▄▄▄▄▄▄▄████
████▄▄▄▀▄▄▄ ▄▀█▀ █▄▄ ███▄█▀████
████▄█▄ ▄ ▄██▀ ▄ ▄█▄▄▀ ▀█▄ ▄████
█████▀▄ █▄ ▀█ ██▄▄ ██ █▄ ████
████▄▀▀▄▀▄▄▄▀ █▀█▄██▀▀▀▄▀█▄ ▄████
████▄█▄▄█▄▄█ █▄▀ ▀█ ▄▄▄ █▀▀████
████ ▄▄▄▄▄ █ ▄█▄ ▀ ▄ █▄█ █ ▄████
████ █ █ █▄▀▄ ██ █ ▄▄ ▀▄▄████
████ █▄▄▄█ █ ▀█▄█▀▀▀▀█ ▀█▀▄████
████▄▄▄▄▄▄▄█▄▄██▄██▄▄▄▄▄███▄▄████
█████████████████████████████████
█████████████████████████████████

get weather info

If you change after /, you can see the weather that you want to know.

$ curl wttr.in/newyork
Weather report: newyork

get cryptocurrency rate

The rate.sx shows you famous currencies' rate. If you put a specific currency, you can see the rate and a chart of the currency.

$ curl rate.sx

Watch animation on Terminal

$ curl ascii.live/nyan

get a page

$ $ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

get method

In this case, I used https://openweathermap.org/ with jq (I will post an article on jq soon).

$ curl "https://api.openweathermap.org/data/2.5/weather?q=London&appid=your_api_key&units=metric" | jq

post method

$ curl -d "param1=aaa&param=bbb" https://api.com

post json

$ curl -H "Content-Type: application/json" \
-d '{"key": "value"}' https://api.com

Basic auth

$ curl -u id:password https://example.com

use Bearer

$ curl -H 'Authorization: Bearer xxxxxxxx' https://api.com

generate a curl command from Google Chrome

Open DevTool > Network > right click name > copy > copy as cURL or copy all as cURL

curl 'chrome-extension://jlmpjdjjbgclbocgajdjefcidcncaied/index.html' --compressed ;
curl 'chrome-extension://jlmpjdjjbgclbocgajdjefcidcncaied/css/home.94d1d1b7.css' \
-H 'Referer: ' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36' \
-H 'DNT: 1' \
--compressed ;
curl 'chrome-extension://jlmpjdjjbgclbocgajdjefcidcncaied/css/standalone/standalone.723be05a.css' \
-H 'Referer: ' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36' \
-H 'DNT: 1' \
--compressed ;
curl 'chrome-extension://jlmpjdjjbgclbocgajdjefcidcncaied/js/home.home.js' \
-H 'Referer: ' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36' \
-H 'DNT: 1' \
--compressed ;
curl 'chrome-extension://jlmpjdjjbgclbocgajdjefcidcncaied/standalone/stand
--compressed

Send Cookie

$ curl -b 'key=value; key2=value2;' https://test.cookie.com
$ curl --cookie 'key=value; key2=value2;' https://test.cookie.com

Save Cookie

$ curl -c cookie.txt https://test.cookie.com

Send back Cookie

Send back stored cookie file, cookie.txt

$ curl -b cookie.txt https://test.cookie.com

Upload file

$ curl -F file_name=@sample.txt https://uploader.com

measure the response (benchmark)

$ curl https://dev.to -s -o /dev/null -w "%{time_total}\n"
0.150539

Only one-time measurement doesn’t make sense lol
The following is do the same thing 10 times and calc the average.

$ for i in {1..10}; do curl https://dev.to -s -o /dev/null -w "%{time_total}\n";done | awk '{sum+=$1;print $1} END {print "AVG: " sum/NR}'

#CreativeCoding #Art #PhysicalComputing #IoT #MachineLearning #python #creativetech

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store