DEV Community

Ali Sherief
Ali Sherief

Posted on

A collection of useful HTTP cheat sheets

Today I will write a quick list of commands for making different type of requests and sending different data types with curl. Curl is very useful for testing APIs to make sure they work properly.

GET request

This one is easy, it's simply curl

For an HTTPS website the equivalent command is curl -k but this is not recommended because it doesn't verify the certificate. Read on to see how to make an HTTPS request.

PUT request with string data

curl -X PUT -d "string data"

PUT request with json data

curl -X PUT -H "Content-Type: application/json" -d {"key": "string data"}

Arbitrary request type


GET request with cookie

curl -b "name=value"

To save the response cookies to a file, use curl -b "name=value" -c file.txt

To load the cookies from a file to a request, using them as request cookies, use curl -b "name=value" -b file.txt

GET request with username and password

curl -u username:password

GET request through proxy

The username and password are optional, if omitted it means log in without authentication. The port number is also optional and defaults to port 1000.

curl -x

Another way to pass a proxy

Set the environment variable export http_proxy=http://USERNAME:PASSWORD@SERVER:PORT/ or export https_proxy=https://USERNAME:PASSWORD@SERVER:PORT/, here the USERNAME and PASSWORD are optional but the SERVER and PORT are mandatory.

This uses the proxy not just for curl but for a host of other Unix programs such as git.

Multipart/form-data upload

Do not use -X PUT to simulate a form data upload because that won't work, you must use the -F option and then specify @ followed by a file path as the value of a key:

curl -F "data=@path/to/local/file"
curl -F 'fileX=@/path/to/fileX' -F 'fileY=@/path/to/fileY

TLS request with curl

First you extract the certificate of the website you are querying. Either export the certificate from the Firefox or Chrome browser window or get it with openssl using

echo quit | openssl s_client -showcerts -servername -connect > cacert.pem

Then you make the request using

curl --cacert cacert.pem

Follow redirects

curl -L

Set the user agent

This example uses the Chrome 83 on 64 bit Windows 10 user agent.

curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"

Decompress result

Sometimes the server will return the response compressed with gzip. If that happens the output you will get on the screen will be binary data, so you have to tell curl to decompress the response before displaying the output, using

curl --compressed

HTTP cheat sheet - everything curl
curl cheatsheet
Upload files with curl - Pete Houston - Medium
How to set up proxy using http_proxy & https_proxy environment variable in Linux?

Top comments (0)