Skip to content
cheatsheetzPublic

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

cURL Cheat Sheet

Command-line tool for transferring data with URLs. Supports HTTP, HTTPS, FTP, and many other protocols.


Table of Contents


Basic Requests

Command Description Example
curl URL GET request curl https://api.example.com
curl -i URL Include response headers curl -i https://api.example.com
curl -I URL Headers only (HEAD request) curl -I https://example.com
curl -L URL Follow redirects curl -L https://bit.ly/example
curl -o file URL Save output to file curl -o data.json https://api.example.com/data
curl -O URL Save with remote filename curl -O https://example.com/file.pdf
curl -s URL Silent mode (no progress) curl -s https://api.example.com
curl -S -s URL Silent but show errors curl -S -s https://api.example.com

HTTP Methods

Command Description Example
curl -X GET URL GET request (default) curl -X GET https://api.example.com/users
curl -X POST URL POST request curl -X POST https://api.example.com/users
curl -X PUT URL PUT request curl -X PUT https://api.example.com/users/1
curl -X DELETE URL DELETE request curl -X DELETE https://api.example.com/users/1
curl -X PATCH URL PATCH request curl -X PATCH https://api.example.com/users/1
curl --head URL HEAD request curl --head https://example.com

Headers & Authentication

Command Description Example
curl -H "Header: value" URL Add header curl -H "Content-Type: application/json" https://api.example.com
curl -H @headers.txt URL Headers from file curl -H @headers.txt https://api.example.com
curl -A "User-Agent" URL Set User-Agent curl -A "Mozilla/5.0" https://example.com
curl -u user:pass URL Basic authentication curl -u admin:secret https://api.example.com
curl -H "Authorization: Bearer TOKEN" URL Bearer token curl -H "Authorization: Bearer abc123" https://api.example.com
curl --cookie "name=value" URL Send cookies curl --cookie "session=abc123" https://example.com
curl -c cookies.txt URL Save cookies curl -c cookies.txt https://example.com/login
curl -b cookies.txt URL Use saved cookies curl -b cookies.txt https://example.com/profile

Data & Forms

JSON Data

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

# JSON from file
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d @user.json

# Pretty print JSON response
curl https://api.example.com/users | python -m json.tool

Form Data

Command Description Example
curl -d "key=value" URL POST form data curl -d "name=John&age=30" https://example.com/form
curl --data-urlencode "key=value" URL URL encode data curl --data-urlencode "message=Hello World!" https://example.com
curl -F "field=@file" URL Upload file (multipart) curl -F "image=@photo.jpg" https://example.com/upload
curl -F "field=value" URL Multipart form data curl -F "name=John" -F "file=@doc.pdf" https://example.com

Download & Upload

Downloading Files

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

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

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

# Download with custom filename
curl -o myfile.zip https://example.com/download.php?id=123

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

Uploading Files

# Upload file with PUT
curl -X PUT -T file.txt https://example.com/upload/file.txt

# Upload with progress bar
curl -# -T large-file.zip https://example.com/upload/

# FTP upload
curl -T file.txt ftp://ftp.example.com/ -u user:pass

Advanced Options

Command Description Example
curl --proxy host:port URL Use proxy curl --proxy localhost:8080 https://example.com
curl -k URL Ignore SSL certificate curl -k https://self-signed.example.com
curl --resolve host:port:addr URL Custom DNS resolution curl --resolve example.com:443:127.0.0.1 https://example.com
curl --connect-timeout 10 URL Connection timeout curl --connect-timeout 10 https://slow-server.com
curl -m 30 URL Maximum time for transfer curl -m 30 https://example.com/large-file
curl --retry 3 URL Retry on failure curl --retry 3 --retry-delay 5 https://unstable-api.com
curl --compressed URL Request compressed response curl --compressed https://api.example.com

Debugging & Info

Command Description Example
curl -v URL Verbose output curl -v https://api.example.com
curl -vv URL Very verbose (with SSL info) curl -vv https://api.example.com
curl -w "@curl-format.txt" URL Custom output format curl -w "Time: %{time_total}\\n" https://example.com
curl --trace trace.txt URL Full trace dump curl --trace trace.txt https://api.example.com
curl --trace-ascii ascii.txt URL ASCII trace dump curl --trace-ascii debug.txt https://api.example.com

Timing Information

# Show detailed timing
curl -w "\\n\\nDNS: %{time_namelookup}\\nConnect: %{time_connect}\\nSSL: %{time_appconnect}\\nTotal: %{time_total}\\n" \
  -o /dev/null -s https://example.com

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

Common Use Cases

API Testing

# GET with query parameters
curl "https://api.example.com/users?page=2&limit=10"

# POST with JSON
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -H "API-Key: your-key" \
  -d '{"name":"Jane","email":"jane@example.com"}'

# PUT to update
curl -X PUT https://api.example.com/users/123 \
  -H "Content-Type: application/json" \
  -d '{"email":"newemail@example.com"}'

# DELETE
curl -X DELETE https://api.example.com/users/123 \
  -H "Authorization: Bearer token123"

Website Testing

# Check if website is up
curl -Is https://example.com | head -n 1

# Test redirect chain
curl -IL https://bit.ly/example

# Check response time
curl -o /dev/null -s -w "Response time: %{time_total}s\\n" https://example.com

# Test with different User-Agent
curl -A "Googlebot" https://example.com

File Operations

# Download and extract tar.gz
curl -L https://example.com/archive.tar.gz | tar xz

# Upload file to FTP
curl -T localfile.txt ftp://ftp.example.com/remote/path/ -u user:pass

# Mirror website (basic)
curl -O https://example.com/index.html

Authentication Examples

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

# OAuth 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

# Certificate authentication
curl --cert client.pem --key client.key https://secure.example.com

Format Options

Custom Output Format

Create curl-format.txt:

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

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

Tips & Tricks

  1. Pipe to jq for JSON: curl -s https://api.example.com | jq .
  2. Save headers separately: curl -D headers.txt -o body.html https://example.com
  3. Use config file: curl -K config.txt where config.txt contains curl options
  4. Parallel downloads: Use xargs or parallel with curl
  5. Test multiple URLs: while read url; do curl -I "$url"; done < urls.txt

Resources


Master HTTP requests and data transfers from the command line.

Releases

Packages

Contributors