The Command-Line URL Tool

  1. What Is curl?



  2. Basic Syntax

  3. curl <options> <URL>

    # Fetch a web page and print to terminal (stdout)
    curl https://example.com
    
    # Fetch over HTTP
    curl http://example.com



  4. Saving Output to a File

  5. # 1) Shell redirection
    curl https://example.com > page.html
    
    # 2) Let curl decide file name from URL with -O (uppercase O)
    curl -O https://example.com/file.zip
    
    # 3) Save to a chosen file name with -o (lowercase o)
    curl -o mypage.html https://example.com



  6. Seeing Response Headers

  7. curl -i https://example.com

    curl -I https://example.com


  8. Following Redirects

  9. curl -L http://example.com



  10. Adding Custom Headers

  11. curl -H "X-My-Header: hello" https://example.com
    
    # Multiple headers:
    curl \
      -H "Authorization: Bearer YOUR_TOKEN" \
      -H "Accept: application/json" \
      https://api.example.com/data


  12. Sending Query Parameters

  13. curl "https://example.com/search?q=haskell&page=2"



  14. HTTP Methods: GET, POST, PUT, DELETE, ...

  15. # Explicit GET
    curl -X GET https://example.com
    
    # DELETE
    curl -X DELETE https://api.example.com/items/123



  16. Sending Form Data (application/x-www-form-urlencoded)

  17. curl -X POST https://example.com/login \
      -d "username=alice&password=secret"

    curl https://example.com/form \
      -d "field1=value1" \
      -d "field2=value2"


  18. Sending JSON Data

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



  20. Uploading Files

  21. curl -X POST https://example.com/upload \
      -F "file=@/path/to/local/file.txt"

    curl https://example.com/upload \
      -F "description=My file" \
      -F "file=@/path/to/file.txt"


  22. Authentication

  23. curl -u "username:password" https://api.example.com/secure

    curl https://api.example.com/data \
      -H "Authorization: Bearer YOUR_TOKEN_HERE"


  24. HTTPS and Certificates

  25. curl -k https://self-signed.example.local



  26. Verbose and Debugging Output

  27. curl -v https://example.com



  28. Setting User-Agent and Other Common Options

  29. curl -A "MyTestClient/1.0" https://example.com



  30. Rate Limiting and Repeated Requests

  31. # Call an endpoint every 5 seconds (Ctrl-C to stop)
    while true; do
      curl -s https://example.com/health
      echo
      sleep 5
    done



  32. Getting Help and Manual

  33. curl --help

    man curl        # on Unix-like systems
    curl --manual   # portable way



Common Options

  1. Basic Output & File Handling Options
  2. Option Description Example
    -h, --help Show a short help summary of available options. curl --help
    -V, --version Show version info and supported protocols/features. curl --version
    -s, --silent Silent mode: no progress meter or error messages (only body on stdout). curl -s https://example.com
    -S, --show-error Show errors even when -s is used (good for scripts). curl -sS https://example.com
    -o <file> Write response body to the given file name. curl -o page.html https://example.com
    -O (capital O) Save to a file named after the remote URL's path (remote file name). curl -O https://example.com/file.zip
    -L, --location Follow HTTP redirects (3xx responses) automatically. curl -L http://example.com
    -# Display a nice progress bar instead of the default meter. curl -# -O https://example.com/big.iso
    -C - Resume a previous download (if server supports HTTP range requests). curl -C - -O https://example.com/big.iso


  3. HTTP Request & Response Control


  4. Option Description Example
    -X <METHOD>, --request <METHOD> Explicitly specify HTTP method: GET, POST, PUT, DELETE, etc. curl -X DELETE https://api.example.com/items/123
    -i, --include Include HTTP response headers in the output (before body). curl -i https://example.com
    -I, --head Send an HTTP HEAD request (get only headers, no body). curl -I https://example.com
    --http1.1, --http2 Force HTTP/1.1 or HTTP/2 for the request (if supported). curl --http2 https://example.com
    --compressed Request a compressed response (gzip, deflate) and automatically decompress it. curl --compressed https://example.com
    --limit-rate <speed> Limit transfer speed (e.g. 200k, 2M). curl --limit-rate 200k -O https://example.com/file.zip


  5. Sending Data: Forms, JSON, Raw Bodies


  6. Option Description Example
    -d <data>, --data <data> Send a request body. Defaults to application/x-www-form-urlencoded and switches method to POST if not set. curl -d "user=alice&pass=secret" https://example.com/login
    --data-urlencode <data> Like -d, but URL-encodes the given data (safe for special characters). curl --data-urlencode "q=haskell & monads" https://example.com/search
    --data-binary <data> Send data exactly as given, without stripping newlines or treating @ specially. curl --data-binary "@/path/to/raw.bin" https://example.com/upload
    -F <name=@file>, --form Send multipart/form-data (file upload) like an HTML form. curl -F "file=@/path/to/photo.jpg" https://example.com/upload
    -G, --get Send the data as query parameters in a GET request instead of POST. curl -G -d "q=haskell" -d "page=2" https://example.com/search


  7. Headers & Authentication Options


  8. Option Description Example
    -H <header>, --header <header> Add a custom HTTP header. Can be used multiple times. curl -H "Accept: application/json" https://api.example.com/data
    -A <agent>, --user-agent <agent> Set a custom User-Agent string. curl -A "MyClient/1.0" https://example.com
    -u <user:pass>, --user <user:pass> Use HTTP Basic authentication (sends Authorization: Basic ... header). curl -u "alice:secret" https://api.example.com/me
    --oauth2-bearer <token> Send OAuth2 Bearer token as Authorization header. curl --oauth2-bearer YOUR_TOKEN https://api.example.com/data
    --referer <url> Set the HTTP Referer header. curl --referer "https://google.com" https://example.com
    -b <file or data>, --cookie Send cookies from a file or directly as a string (client side). curl -b "session=abcd1234" https://example.com
    -c <file>, --cookie-jar Save cookies to a file after the request (for reuse later). curl -c cookies.txt https://example.com/login


  9. TLS / HTTPS & Security Options


  10. Option Description Example
    -k, --insecure Skip certificate verification (accept self-signed/invalid certs). Useful for local dev, not recommended in production. curl -k https://localhost:8443
    --cacert <file> Use a custom CA certificate file to verify the server. curl --cacert myCA.pem https://internal.example.com
    --cert <cert> Provide a client certificate (for mutual TLS authentication). curl --cert mycert.pem https://secure.example.com
    --key <key> Private key corresponding to --cert, if stored separately. curl --cert client.crt --key client.key https://secure.example.com
    --tlsv1.2, --tlsv1.3 Force a specific TLS version (useful for debugging or strict policies). curl --tlsv1.2 https://example.com


  11. Debugging & Performance Options


  12. Option Description Example
    -v, --verbose Verbose mode: show request headers, response headers, and connection info. curl -v https://example.com
    --trace <file> Write a very detailed trace of everything curl does into a file (binary safe). curl --trace trace.log https://example.com
    --trace-ascii <file> Like --trace, but easier to read (ASCII only). curl --trace-ascii debug.txt https://example.com
    --max-time <seconds> Limit the total allowed time for the whole transfer. curl --max-time 10 https://slow.example.com
    --connect-timeout <seconds> Limit how long curl waits to establish the connection. curl --connect-timeout 5 https://example.com
    --retry <n> Automatically retry the request up to n times on transient errors. curl --retry 3 https://flaky.example.com
    --retry-delay <seconds> Wait this many seconds between retries. curl --retry 3 --retry-delay 5 https://flaky.example.com


  13. Common Option Combinations (Mini Cheat Sheet)

  14. # 1) Simple JSON API GET request with custom header
    curl -sS -H "Accept: application/json" https://api.example.com/users
    
    # 2) JSON POST with Bearer token
    curl -sS -X POST https://api.example.com/users \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer YOUR_TOKEN" \
      -d '{"name":"Alice","age":30}'
    
    # 3) Follow redirects, save result to file
    curl -L -o page.html https://example.com
    
    # 4) Debug a failing HTTPS endpoint (verbose + do not verify cert)
    curl -v -k https://localhost:8443
    
    # 5) Upload a file with additional form data
    curl -F "file=@/path/to/file.txt" \
         -F "description=My upload" \
         https://example.com/upload