Curl Cheatsheet

HTTP methods, headers, auth, cookies, download & API testing

Linux / Command
Contents
๐ŸŒ

Basic Requests

curl https://api.example.com
curl -s URL                     # silent
curl -v URL                     # verbose
curl -o out.html URL            # save to file
curl -O URL/file.zip            # keep name
curl -L URL                     # follow redirects
curl -I URL                     # headers only
curl -w "\nHTTP %{http_code}\n" URL
๐Ÿ“ก

HTTP Methods

# POST
curl -X POST URL \
  -H "Content-Type: application/json" \
  -d '{"name":"Alice"}'

# PUT
curl -X PUT URL/1 -H "Content-Type: application/json" \
  -d '{"name":"Updated"}'

# PATCH
curl -X PATCH URL/1 -d '{"status":"active"}'

# DELETE
curl -X DELETE URL/1
๐Ÿ”‘

Headers & Auth

curl -H "Content-Type: application/json" \
     -H "Accept: application/json" URL

# Bearer token
curl -H "Authorization: Bearer <token>" URL

# Basic auth
curl -u user:pass URL

# API key
curl -H "X-API-Key: abc123" URL
๐Ÿ“ค

Sending Data

# JSON from file
curl -X POST URL -H "Content-Type: application/json" -d @data.json

# Form data
curl -X POST URL -d "name=Alice&email=a@b.com"

# Multipart file upload
curl -X POST URL -F "file=@photo.jpg" -F "name=Alice"

# URL encode
curl --data-urlencode "q=hello world" URL
โฌ‡๏ธ

Download & Upload

curl -O URL/file.zip
curl -o my.zip URL/file.zip
curl -C - -O URL/large.iso       # resume
curl --limit-rate 1M -O URL
curl -T file.txt ftp://ftp.example.com/
๐Ÿช

Cookies

curl -b "session=abc" URL
curl -c cookies.txt URL           # save
curl -b cookies.txt URL           # use saved
curl -c jar.txt -b jar.txt URL    # save & use
โš™๏ธ

Advanced

curl --connect-timeout 5 --max-time 10 URL
curl --retry 3 --retry-delay 2 URL
curl -x http://proxy:8080 URL
curl -k URL                       # skip SSL verify
curl --compressed URL

# Timing
curl -w "DNS:%{time_namelookup}s Total:%{time_total}s\n" \
  -o /dev/null -s URL
๐Ÿงช

API Testing

# Pretty JSON (pipe to jq)
curl -s URL/users | jq .
curl -s URL/users | jq '.[0].name'

# Status code only
curl -s -o /dev/null -w "%{http_code}" URL

# GraphQL
curl -X POST URL/graphql \
  -H "Content-Type: application/json" \
  -d '{"query":"{ users { id name } }"}'