What I Wanted to Do

I wanted to send HTTP requests from the command line. With curl, you can test APIs and download files.

Environment

  • Linux / Mac / Windows (Git Bash)

Basic Usage

GET request

curl https://example.com
curl -s https://example.com    # Hide progress output
curl -o output.html https://example.com  # Save to a file

POST request

curl -X POST https://api.example.com/data \
  -H "Content-Type: application/json" \
  -d '{"key": "value"}'

Checking headers

curl -I https://example.com    # Show headers only
curl -v https://example.com    # Verbose output

Authenticated request

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

Commonly Used API Testing Options

-X GET/POST/PUT/DELETE   # Specify HTTP method
-H "Header-Name: value"  # Add a header
-d "data"                # Request body
-o filename              # Save response to a file
-s                       # Silent mode
-v                       # Verbose output
-L                       # Follow redirects

Common Pitfalls

  • Single quotes don’t work in the Windows Command Prompt. Use Git Bash instead
  • Combining -s and -o saves to a file without showing progress
  • To pretty-print JSON output, pipe to | python3 -m json.tool

Once you’ve confirmed an API returns the correct response, combining it with How to Search Files with grep and find on Linux to filter results through a pipe is a useful technique to know.

Looking for reliable cloud infrastructure? Check out these developer-friendly services.