cURL Cheat Sheet

Curl is an awesome tool. I keep forgetting the switches though, so here’s a cheatsheet.

Simple GET request

curl -k "https://localhost/foo?bar=baz&abc=def"

JSON POST or PUT request

curl -k -H "Content-Type: application/json" -X POST -d '{"accountName":"test","value":"hello"}' https://localhost/foo
curl -X "PUT"
... for a PUT

POST a file

curl ... --data-binary @filename

Fake a /etc/hosts entry and a Host: header with curl

curl -vvv --resolve 'book.mixu.net:80:123.145.167.189' https://book.mixu.net/

Make a request with basic auth enabled

curl -vvv -u [email protected]:password https://www.example.com

or:

curl --user name:password https://www.example.com

Set the Referer header

curl -e https://curl.haxx.se daniel.haxx.se

Set the User Agent header

curl -A "Mozilla/4.73"

or

curl --user-agent "Mozilla".

Set Cookies

curl -b "name=Daniel"

or

curl --cookie "name=Daniel"

Time a request (connect time + time to first byte + total tile)

curl -o /dev/null -w "Connect: %{time_connect} TTFB: %{time_starttransfer} Total time: %{time_total} \n" https://google.com

Downloading files from Github

curl -O  https://raw.github.com/username/reponame/master/filename

brian4286

Brian has spent more than fifteen years as Unix enthusiast, specalising in systems administration and solutions architecture for SMB's to Fortune 100 companies. His career has focused on emerging and niche technologies to major advancements in the industry - most notibly cloud.