curl Command Examples & Cheat Sheet — The Complete Guide

Published: June 2025 • 15 min read • Category: Dev Tools

curl is one of the most essential tools in every developer's toolkit. Whether you're debugging APIs, downloading files, testing server endpoints, or automating HTTP requests, curl is your go-to command-line utility. This guide covers 50+ practical curl examples organized by use case so you can quickly find what you need.

What is curl?

curl (short for "Client URL") is a command-line tool and library for transferring data with URLs. It supports HTTP, HTTPS, FTP, SFTP, SCP, SMTP, and dozens of other protocols. Originally released in 1998 by Daniel Stenberg, curl is pre-installed on virtually every Linux, macOS, and Windows 10+ system.

Basic Syntax

The fundamental syntax of curl is straightforward:

curl [options] [URL]

By default, curl performs an HTTP GET request and prints the response body to stdout.

1. GET Requests

Simple GET

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

GET with Query Parameters

curl "https://api.example.com/users?page=1&limit=10"

GET with Headers

curl -H "Authorization: Bearer YOUR_TOKEN" \
     -H "Accept: application/json" \
     https://api.example.com/users

Verbose GET (show request and response details)

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

Show Response Headers Only

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

Show Both Headers and Body

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

2. POST Requests

POST JSON Data

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

POST Form Data

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

POST File Upload

curl -X POST https://api.example.com/upload \
  -F "file=@/path/to/document.pdf" \
  -F "description=Quarterly report"
Tip: Use -F for multipart/form-data uploads (common in file uploads), and -d for application/x-www-form-urlencoded or raw data.

3. PUT and PATCH Requests

PUT (full update)

curl -X PUT https://api.example.com/users/42 \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice Updated", "email": "alice@new.com"}'

PATCH (partial update)

curl -X PATCH https://api.example.com/users/42 \
  -H "Content-Type: application/json" \
  -d '{"email": "alice@new.com"}'

4. DELETE Requests

curl -X DELETE https://api.example.com/users/42 \
  -H "Authorization: Bearer YOUR_TOKEN"

5. Authentication

Basic Authentication

curl -u username:password https://api.example.com/protected

Bearer Token

curl -H "Authorization: Bearer eyJhbGciOiJIUzI1..." \
     https://api.example.com/me

For debugging JWT tokens, use our free JWT Decoder tool to inspect token payloads.

OAuth 2.0 Client Credentials

curl -X POST https://auth.example.com/token \
  -d "grant_type=client_credentials" \
  -d "client_id=YOUR_ID" \
  -d "client_secret=YOUR_SECRET"

API Key in Header

curl -H "X-API-Key: your-api-key-here" \
     https://api.example.com/data

6. Following Redirects

By default, curl does not follow redirects. Use -L to follow them:

curl -L https://example.com/redirect-endpoint

Limit Redirect Follows

curl -L --max-redirs 3 https://example.com

7. Downloading Files

Save to a File with Original Name

curl -O https://example.com/file.zip

Save with Custom Filename

curl -o myfile.zip https://example.com/file.zip

Resume a Partial Download

curl -C - -O https://example.com/large-file.iso

Download with Progress Bar

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

Silent Download (no progress output)

curl -s -O https://example.com/file.zip
Tip: Use -sS (silent + show errors) in scripts so you suppress progress output but still see error messages.

8. Uploading Files

Upload via PUT

curl -X PUT -T /path/to/file.txt \
     https://storage.example.com/uploads/file.txt

Upload Multiple Files

curl -X POST https://api.example.com/upload \
  -F "file1=@photo.jpg" \
  -F "file2=@document.pdf" \
  -F "metadata={\"tags\":[\"report\"]};type=application/json"

9. Headers Management

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

# Set multiple headers
curl -H "Accept: application/json" \
     -H "X-Request-ID: abc123" \
     https://api.example.com

# Remove a default header
curl -H "User-Agent:" https://api.example.com

# Send a custom User-Agent
curl -A "MyApp/1.0" https://api.example.com

10. Handling Cookies

Save Cookies to a File

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

Send Cookies from a File

curl -b cookies.txt https://example.com/dashboard

Set a Single Cookie

curl -b "session_id=abc123" https://example.com

11. SSL/TLS Options

Skip SSL Verification (for testing only!)

curl -k https://self-signed.example.com/api

Specify a CA Certificate

curl --cacert /path/to/ca.crt https://example.com

Use Client Certificate

curl --cert client.crt --key client.key \
     https://example.com/api
Warning: Never use -k (insecure) in production. It disables all SSL certificate verification and exposes you to man-in-the-middle attacks.

12. Rate Limiting & Timeout

Set Connection Timeout

curl --connect-timeout 10 https://api.example.com

Set Maximum Transfer Time

curl --max-time 30 https://api.example.com

Rate Limit Requests

curl --rate 10/m https://api.example.com/data

13. Proxy Usage

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

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

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

14. Testing & Debugging

View Full Request/Response

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

View Only Response Headers

curl -s -D - https://api.example.com -o /dev/null

Measure Response Time

curl -w "time_total: %{time_total}s\n" -s -o /dev/null https://api.example.com

Extract Specific Response Info

curl -s -w "HTTP Code: %{http_code}\nTotal Time: %{time_total}s\nSize: %{size_download} bytes\n" \
  -o /dev/null https://api.example.com

Pretty Print JSON Response

curl -s https://api.example.com/users | python3 -m json.tool

For formatting JSON online, try our JSON Formatter & Beautifier.

15. Common curl Options Reference

OptionDescriptionExample
-XHTTP method-X POST
-HRequest header-H "Content-Type: application/json"
-dPOST data-d '{"key":"value"}'
-FMultipart form upload-F "file=@photo.jpg"
-oWrite output to file-o output.html
-OSave with remote filename-O
-LFollow redirects-L
-uBasic auth-u user:pass
-bSend cookies-b "name=value"
-cSave cookies-c cookies.txt
-sSilent mode-s
-vVerbose output-v
-kSkip SSL verification-k
-iInclude response headers-i
-IHEAD request (headers only)-I
-AUser-Agent string-A "MyBot/1.0"
-C -Resume download-C - -O url
-wFormat output with variables-w "%{http_code}"
--data-binarySend binary data--data-binary @file.bin
--compressedRequest compressed response--compressed

16. curl vs Other Tools

While curl is the king of CLI HTTP, for interactive API testing, tools like Postman and HTTP status code references can be helpful. Here's when to use what:

17. Common curl Mistakes to Avoid

  1. Forgetting quotes around JSON: Always wrap JSON in single quotes to prevent shell expansion.
  2. Not using -H "Content-Type: application/json": Many APIs reject requests without the correct Content-Type.
  3. Forgetting -L for redirects: APIs using OAuth flows often redirect, and curl won't follow by default.
  4. Using -k in production: Disabling SSL verification is dangerous.
  5. Not escaping & in URLs: Shell interprets & as a background operator — always quote URLs with &.

18. Advanced: Using curl in Bash Scripts

Here's a practical example of using curl in a script to check website health and extract response data:

#!/bin/bash
# health_check.sh — Monitor API endpoints

ENDPOINTS=(
  "https://api.example.com/health"
  "https://api.example.com/users"
)

for url in "${ENDPOINTS[@]}"; do
  HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$url")
  TIME=$(curl -s -o /dev/null -w "%{time_total}" --max-time 10 "$url")
  
  if [ "$HTTP_CODE" -ge 200 ] && [ "$HTTP_CODE" -lt 300 ]; then
    echo "✅ $url — HTTP $HTTP_CODE (${TIME}s)"
  else
    echo "❌ $url — HTTP $HTTP_CODE (${TIME}s)"
  fi
done

Conclusion

curl is an incredibly powerful and versatile tool that every developer should master. Whether you're building REST APIs, debugging authentication flows, automating deployments, or simply downloading files, the examples in this cheat sheet cover the most common scenarios. Bookmark this page and keep it handy — you'll reach for these curl commands daily.

For more developer tools and guides, explore the Hedpy Blog or check out our free online developer tools.

🛠️ 推荐工具

免费在线开发工具,浏览器直接使用

前往工具站 → nbsp; AI API接口 →