Tips&Tricks

Curl basic usage and examples

terminal

cUrl is a powerful tool for creating HTTP requests. If you need to make almost any HTTP request from console – use CURL! So, let’s see some curl’s keys:

Options:

-o {{ file }} # --output: write to file 
-u user:pass # --user: Authentication
-v # --verbose
-vv # Even more verbose
-I # --head: headers only

Request:

-X POST          # --request

Data:

-d 'data'    # --data: HTTP post data, URL encoded (eg, status="Hello") 
-d @file # --data via file
-G # --get: send -d data via get

Headers:

-A {{ str }}     # --user-agent 
-b name=val # --cookie
-b FILE # --cookie
-H "X-Foo: y" # --header
--compressed # use deflate/gzip

SSL:

--cacert {{ file }} 
--capath {{ dir }}
-E, --cert {{ cert }} # --cert: Client cert file
--cert-type # der/pem/eng
-k, --insecure # for self-signed certs

Examples

# Post data:
curl -d password=x http://x.com/y

# Auth/data:
curl -u user:pass -d status="Hello"
http://twitter.com/statuses/update.xml

# multipart file upload
curl -v -include --form key1=value1
--form upload=@localfilename {{ URL }}

# JSON-request via POST
curl -d '{"key1":"value1", "key2":"value2"}'
-H "Content-Type: application/json"
-X POST {{ URL }}

It’s basic usage of cUrl. Use it, cUrl is cute!