← Back to all cheatsheets
DevOps
curlhttpapiclinetworking

curl Cheat Sheet

Basic Syntax

curl [options] [URL]

Common HTTP Methods

GET Request (default)

curl https://api.example.com/users

POST Request

curl -X POST https://api.example.com/users
curl -d "name=John&age=30" https://api.example.com/users
curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":30}' https://api.example.com/users

PUT Request

curl -X PUT -d "updated data" https://api.example.com/users/1

DELETE Request

curl -X DELETE https://api.example.com/users/1

PATCH Request

curl -X PATCH -d "partial update" https://api.example.com/users/1

Headers

Add Custom Header

curl -H "Authorization: Bearer token123" https://api.example.com
curl -H "Accept: application/json" -H "Content-Type: application/json" https://api.example.com

View Response Headers

curl -i https://example.com          # Include headers in output
curl -I https://example.com          # Headers only (HEAD request)
curl -v https://example.com          # Verbose (shows request and response headers)

Authentication

Basic Authentication

curl -u username:password https://api.example.com
curl -u username https://api.example.com    # Prompts for password

Bearer Token

curl -H "Authorization: Bearer your_token_here" https://api.example.com

API Key

curl -H "X-API-Key: your_api_key" https://api.example.com
curl https://api.example.com?api_key=your_api_key

Data Transfer

Send JSON Data

curl -X POST -H "Content-Type: application/json" \
  -d '{"key":"value"}' https://api.example.com

Send Form Data

curl -X POST -F "field1=value1" -F "field2=value2" https://api.example.com

Send File

curl -F "file=@/path/to/file.txt" https://api.example.com/upload
curl -T /path/to/file.txt https://api.example.com/upload    # PUT file

Send Data from File

curl -d @data.json -H "Content-Type: application/json" https://api.example.com

Output Options

Save Response to File

curl -o output.html https://example.com           # Save with specified name
curl -O https://example.com/file.pdf              # Save with original name

Follow Redirects

curl -L https://example.com                       # Follow redirects
curl -L --max-redirs 3 https://example.com        # Limit redirects

Silent Mode

curl -s https://example.com                       # Silent mode
curl -sS https://example.com                      # Silent but show errors

Debugging & Troubleshooting

Verbose Output

curl -v https://example.com                       # Verbose mode
curl -vv https://example.com                      # Even more verbose

Show Only HTTP Code

curl -s -o /dev/null -w "%{http_code}" https://example.com

Time Analysis

curl -w "@curl-format.txt" -o /dev/null -s https://example.com

Create curl-format.txt:

time_namelookup:  %{time_namelookup}\n
time_connect:     %{time_connect}\n
time_appconnect:  %{time_appconnect}\n
time_redirect:    %{time_redirect}\n
time_pretransfer: %{time_pretransfer}\n
time_starttransfer: %{time_starttransfer}\n
time_total:       %{time_total}\n

Cookies

Save Cookies

curl -c cookies.txt https://example.com

Send Cookies

curl -b cookies.txt https://example.com
curl -b "name=value; name2=value2" https://example.com

Proxy

Use Proxy

curl -x http://proxy:8080 https://example.com
curl --proxy http://user:pass@proxy:8080 https://example.com

SOCKS Proxy

curl --socks5 localhost:9050 https://example.com

SSL/TLS

Ignore SSL Certificate

curl -k https://example.com                       # Insecure (skip verification)

Use Client Certificate

curl --cert client.pem --key client-key.pem https://example.com

Specify CA Certificate

curl --cacert ca-bundle.crt https://example.com

Timeouts & Limits

Set Timeout

curl --connect-timeout 10 https://example.com     # Connection timeout
curl --max-time 30 https://example.com            # Total time limit

Limit Transfer Rate

curl --limit-rate 200K https://example.com        # Limit to 200KB/s

Useful Combinations

API Testing with Pretty JSON

curl -s https://api.example.com/data | jq '.'

Download with Progress Bar

curl -# -O https://example.com/largefile.zip

Multiple URLs

curl -O https://example.com/file1.txt -O https://example.com/file2.txt

Resume Download

curl -C - -O https://example.com/largefile.zip

Check Website Status

curl -Is https://example.com | head -n 1

POST JSON with Response Headers

curl -i -X POST -H "Content-Type: application/json" \
  -d '{"key":"value"}' https://api.example.com

Environment Variables

Use .netrc for Authentication

# Create ~/.netrc file:
# machine api.example.com login username password secret123
curl -n https://api.example.com

Export Variables

export CURL_CA_BUNDLE=/path/to/ca-bundle.crt

Common Options Reference

OptionLong FormDescription
-X—requestHTTP method
-d—dataSend data
-H—headerAdd header
-i—includeInclude headers in output
-I—headHEAD request
-o—outputOutput to file
-O—remote-nameSave with remote name
-u—userAuthentication
-F—formMultipart form data
-L—locationFollow redirects
-v—verboseVerbose output
-s—silentSilent mode
-k—insecureAllow insecure SSL
-c—cookie-jarSave cookies
-b—cookieSend cookies
-A—user-agentUser agent string
-e—refererReferer URL
-x—proxyUse proxy
-w—write-outFormat output
-T—upload-fileUpload file

Tips

  1. Use -v for debugging connection issues
  2. Combine -s and -S for scripts (silent but show errors)
  3. Use -w "\n" to add newline after output
  4. Quote URLs with special characters (&, ?, etc.)
  5. Use --compressed to request compressed response
  6. Test APIs with curl -X OPTIONS to see allowed methods
  7. Use --trace-ascii - for detailed debugging output