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 http://example.com.

For an HTTPS website the equivalent command is curl -k https://example.com 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" http://example.com

PUT request with json data

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

Arbitrary request type

curl -X <POST/HEAD/PUT/GET> http://example.com

GET request with cookie

curl -b "name=value" https://example.com

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

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

GET request with username and password

curl -u username:password http://example.com

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 user:password@proxyhost.com:port http://example.com

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" http://example.com
curl -F 'fileX=@/path/to/fileX' -F 'fileY=@/path/to/fileY http://example.com

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 example.com -connect example.com:443 > cacert.pem

Then you make the request using

curl --cacert cacert.pem https://example.com

Follow redirects

curl -L http://example.com

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" http://example.com

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://example.com


Credits:
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?

Oldest comments (0)