Terminal GuideTerminal Guide

curl Command Guide

curl is a powerful command-line tool for transferring data with URLs. Learn how to make HTTP requests, download files, and interact with APIs.

8 min readLast updated: January 19, 2025
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

curl URLFetch URL content
curl -o file URLSave to file
curl -O URLSave with original name

HTTP Methods

-X POSTPOST request
-X PUTPUT request
-X DELETEDELETE request

Data

-d "data"Send POST data
-d @file.jsonSend file as data
-F "file=@f.txt"Upload file

Headers

-H "Key: Value"Add header
-H "Content-Type: application/json"JSON content
-u user:passBasic auth

Options

-LFollow redirects
-IHeaders only
-vVerbose output

Download

-C -Resume download
--limit-rate 1MLimit speed
-kInsecure (skip SSL)

Downloadable Image Preview

Failed to generate preview

Basic Usage

curl fetches content from a URL and outputs it to stdout.

bash
curl https://example.com

Common Options

Frequently Used Options

-o fileSave output to file
-OSave with remote filename
-LFollow redirects
-IFetch headers only
-vVerbose output
-sSilent mode
-X METHODSpecify HTTP method
-HAdd custom header
-dSend data in POST request

Downloading Files

Save to specific file

bash
curl -o myfile.html https://example.com/page.html

Save with original filename

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

Download multiple files

bash
curl -O https://example.com/file1.zip -O https://example.com/file2.zip

Resume interrupted download

bash
curl -C - -O https://example.com/largefile.zip

Follow redirects

bash
curl -L -O https://example.com/download
Tip
Always use -L to follow redirects, as many URLs redirect to the actual content.

HTTP Methods

GET request (default)

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

POST request with data

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

POST with JSON data

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

PUT request

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

DELETE request

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

Headers and Authentication

Add custom headers

bash
curl -H "Authorization: Bearer token123" \
  -H "Accept: application/json" \
  https://api.example.com/data

Basic authentication

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

View response headers

bash
# Headers only
curl -I https://example.com

# Headers and body
curl -i https://example.com
Warning
Avoid putting sensitive credentials in command history. Use environment variables or config files for authentication tokens.

Working with APIs

Pretty print JSON (with jq)

bash
curl -s https://api.example.com/data | jq .

Send JSON file as data

bash
curl -X POST \
  -H "Content-Type: application/json" \
  -d @data.json \
  https://api.example.com/endpoint

Upload a file

bash
curl -X POST \
  -F "file=@myfile.txt" \
  https://api.example.com/upload

Debugging and Verbose Output

bash
# Verbose output showing request/response details
curl -v https://example.com

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

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

Practical Examples

Check if a website is up

bash
curl -Is https://example.com | head -1

Get your public IP

bash
curl ifconfig.me

Download a script and run it

bash
curl -fsSL https://example.com/install.sh | bash
Warning
Be careful when piping scripts to bash. Always review the script first with curl URL before executing.

Test API with rate limiting

bash
curl --limit-rate 100K https://example.com/largefile

Use a proxy

bash
curl -x http://proxy:8080 https://example.com

Ignore SSL certificate errors

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

Summary

curl is essential for HTTP requests and data transfer. Key takeaways:

  • Use -o or -O to save downloads
  • Use -L to follow redirects
  • Use -X to specify HTTP method
  • Use -H for custom headers
  • Use -d to send POST data
  • Use -v for debugging

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles