Curl

Essential curl commands for making HTTP requests and transferring data.

cli
curlhttpapiterminalnetworking

Basic Requests

# Simple GET request
curl https://api.example.com/users

# Save output to file
curl -o output.html https://example.com
curl -O https://example.com/file.zip  # Keep original filename

# Follow redirects
curl -L https://example.com

# Show response headers
curl -i https://api.example.com/users

# Show only headers (HEAD request)
curl -I https://api.example.com/users

# Silent mode (no progress bar)
curl -s https://api.example.com/users

# Verbose output (debugging)
curl -v https://api.example.com/users

HTTP Methods

# GET (default)
curl https://api.example.com/users

# POST with data
curl -X POST https://api.example.com/users \
  -d "name=John&email=john@example.com"

# POST JSON data
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name": "John", "email": "john@example.com"}'

# PUT request
curl -X PUT https://api.example.com/users/1 \
  -H "Content-Type: application/json" \
  -d '{"name": "Jane"}'

# PATCH request
curl -X PATCH https://api.example.com/users/1 \
  -H "Content-Type: application/json" \
  -d '{"email": "jane@example.com"}'

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

Headers

# Add custom header
curl -H "Authorization: Bearer token123" https://api.example.com

# Multiple headers
curl -H "Authorization: Bearer token123" \
     -H "Accept: application/json" \
     -H "X-Custom-Header: value" \
     https://api.example.com

# Set User-Agent
curl -A "MyApp/1.0" https://api.example.com
curl -H "User-Agent: MyApp/1.0" https://api.example.com

# Set Accept header
curl -H "Accept: application/json" https://api.example.com

Authentication

# Basic authentication
curl -u username:password https://api.example.com
curl --user username:password https://api.example.com

# Bearer token
curl -H "Authorization: Bearer your_token" https://api.example.com

# API key in header
curl -H "X-API-Key: your_api_key" https://api.example.com

# Digest authentication
curl --digest -u username:password https://api.example.com

# Netrc file authentication
curl -n https://api.example.com  # Uses ~/.netrc

Data & Forms

# URL-encoded form data
curl -d "field1=value1&field2=value2" https://api.example.com/form

# Read data from file
curl -d @data.json https://api.example.com/users
curl --data-binary @image.png https://api.example.com/upload

# Form data with file upload (multipart)
curl -F "file=@photo.jpg" https://api.example.com/upload
curl -F "file=@photo.jpg;type=image/jpeg" https://api.example.com/upload

# Multiple files and fields
curl -F "file1=@photo1.jpg" \
     -F "file2=@photo2.jpg" \
     -F "name=John" \
     https://api.example.com/upload

# URL encode data
curl --data-urlencode "message=Hello World!" https://api.example.com

Cookies

# Send cookies
curl -b "session=abc123" https://example.com
curl --cookie "name=value; name2=value2" https://example.com

# Save cookies to file
curl -c cookies.txt https://example.com

# Load cookies from file
curl -b cookies.txt https://example.com

# Save and load cookies (session)
curl -c cookies.txt -b cookies.txt https://example.com

SSL/TLS

# Skip certificate verification (insecure)
curl -k https://self-signed.example.com
curl --insecure https://self-signed.example.com

# Use specific certificate
curl --cert client.pem https://api.example.com
curl --cert client.pem:password https://api.example.com

# Use certificate authority
curl --cacert ca-bundle.crt https://api.example.com

# Use client key
curl --cert client.crt --key client.key https://api.example.com

# Specify TLS version
curl --tlsv1.2 https://api.example.com
curl --tlsv1.3 https://api.example.com

Timeouts & Limits

# Connection timeout (seconds)
curl --connect-timeout 10 https://api.example.com

# Maximum time for operation
curl -m 30 https://api.example.com
curl --max-time 30 https://api.example.com

# Limit download speed
curl --limit-rate 100K https://example.com/large-file.zip

# Retry on failure
curl --retry 3 https://api.example.com
curl --retry 3 --retry-delay 5 https://api.example.com

Proxy

# Use HTTP proxy
curl -x http://proxy.example.com:8080 https://api.example.com
curl --proxy http://proxy.example.com:8080 https://api.example.com

# SOCKS5 proxy
curl --socks5 proxy.example.com:1080 https://api.example.com

# Proxy with authentication
curl -x http://user:pass@proxy.example.com:8080 https://api.example.com

# Bypass proxy for specific hosts
curl --noproxy "localhost,*.local" https://api.example.com

Output Formatting

# Write output to file
curl -o output.json https://api.example.com/data

# Append to file
curl https://api.example.com/data >> output.json

# Format JSON with jq
curl -s https://api.example.com/data | jq '.'

# Show timing information
curl -w "Time: %{time_total}s\n" -o /dev/null -s https://example.com

# Detailed timing breakdown
curl -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTLS: %{time_appconnect}s\nTotal: %{time_total}s\n" \
  -o /dev/null -s https://example.com

# Show HTTP status code only
curl -s -o /dev/null -w "%{http_code}" https://api.example.com

Downloading

# Download file with original name
curl -O https://example.com/file.zip

# Download multiple files
curl -O https://example.com/file1.zip -O https://example.com/file2.zip

# Resume interrupted download
curl -C - -O https://example.com/large-file.zip

# Download with progress bar
curl -# -O https://example.com/file.zip

# Download only if modified
curl -z "2024-01-01" https://example.com/file.txt

Advanced Usage

# Send request from file
curl -K request.txt  # Config file with curl options

# Multiple requests
curl https://api.example.com/users https://api.example.com/posts

# Use variables in URL
curl "https://api.example.com/users/[1-10]"  # Sequential
curl "https://api.example.com/users/{1,5,10}"  # Specific values

# Compressed response
curl --compressed https://api.example.com

# HTTP/2
curl --http2 https://api.example.com

# Show request as it would be sent
curl -v --trace-ascii - https://api.example.com 2>&1 | head -50

# Test if URL exists (no output)
curl -fs https://example.com && echo "OK" || echo "Failed"

Common Patterns

# POST JSON and parse response
curl -s -X POST https://api.example.com/login \
  -H "Content-Type: application/json" \
  -d '{"user": "admin", "pass": "secret"}' | jq '.token'

# Download with authentication
curl -u user:pass -O https://secure.example.com/file.zip

# Health check endpoint
curl -sf http://localhost:8080/health || exit 1

# API pagination
curl "https://api.example.com/items?page=1&limit=100"

# Webhook testing
curl -X POST https://webhook.example.com \
  -H "Content-Type: application/json" \
  -d '{"event": "test", "data": {"key": "value"}}'

Options Reference

OptionLong FormDescription
-X--requestSpecify HTTP method (GET, POST, PUT, DELETE, etc.)
-H--headerAdd custom header to request
-d--dataSend data in POST request body
-F--formSubmit form data (multipart/form-data)
-o--outputWrite output to file instead of stdout
-O--remote-nameSave file with remote filename
-i--includeInclude response headers in output
-I--headFetch headers only (HEAD request)
-s--silentSilent mode, hide progress and errors
-S--show-errorShow errors even in silent mode
-v--verboseVerbose output for debugging
-L--locationFollow redirects
-u--userServer user and password
-A--user-agentSet User-Agent header
-b--cookieSend cookies from string or file
-c--cookie-jarSave cookies to file
-k--insecureAllow insecure SSL connections
-m--max-timeMaximum time for operation (seconds)
-x--proxyUse proxy server
-w--write-outDisplay info after transfer
-C--continue-atResume transfer at offset
-#--progress-barShow progress as a bar
-f--failFail silently on HTTP errors
-n--netrcUse .netrc for credentials