Roozky's Wonders

Blogging about programming

Mastering cURL by Example

This is a list of cURL commands I found myself using regularly. More examples with deeper explanation can be found here

If you find yourself sitting behind a proxy

If you find yourself on a computer that is sitting behind a proxy:

1
curl --proxy http://proxy.example.org:4321 http://remote.example.org/

Form submit with data url-encoded manually

Simulates a form submission by POST with application/x-www-form-urlencoded content type and data url-encoded by you.

1
curl -d "year=1905&name=You+Me" http://www.example.com/form-submit

Form submit with data url-encoding automatically

Simulates a form submission by POST with application/x-www-form-urlencoded content type and data url-encoded by curl itself.

1
curl --data-urlencode "name=Joe Me" http://www.example.com/form-submit

POST with JSON body

1
curl -H "Content-Type: application/json" -d "{'happy': 'yes'}" http://www.example.com/json

Take a body from stdin

1
echo '{"text": "Hello **world**!"}' | curl -d @- https://api.github.com/markdown

Output HTTP status only

1
curl  http://www.google.com -sL -w "%{http_code}\\n" -o /dev/null

The key flat here is -w which prints the report using a custom format. Here is a list of available custom variables:

url_effective
http_code
http_connect
time_total
time_namelookup
time_connect
time_pretransfer
time_redirect
time_starttransfer
size_download
size_upload
size_header
size_request
speed_download
speed_upload
content_type
num_connects
num_redirects
ftp_entry_path

Check status of multiple URLs

1
for i in gumtree bbc; do curl  http://www.$i.com -sL -w "%{http_code} %{url_effective}\\n" -o /dev/null; done

Outputs:

1
2
200 http://www.gumtree.com/
200 http://www.bbc.com/