Curl Basics

Beginner-friendly, W3Schools-style walkthrough: short lesson, quick example, copy, run, repeat. Start at Lesson 1 and work down the page.

Lesson 1: Make a basic GET request

Use curl with a URL to request data.

curl https://api.github.com
Copied!

Lesson 2: Follow redirects

Some sites redirect you. Add -L so curl follows the redirect.

curl -L http://github.com
Copied!

Find the final URL after redirects without downloading the page body:

curl -Ls -o /dev/null -w "%{url_effective}\n" http://example.com
curl -Ls -o NUL -w "%{url_effective}\n" http://example.com
Copied!

Lesson 3: Save output to a file

Use -o to set a filename or -O to keep the remote filename.

curl -L https://example.com -o page.html
curl -L https://example.com/file.zip -O
Copied!

Lesson 4: View response headers

Use -I for headers only, or -i for headers + body.

curl -I https://example.com
curl -i https://example.com
Copied!

Lesson 5: Send custom headers

Use -H to send headers like Accept or API keys.

curl -H "Accept: application/json" https://api.github.com/users/octocat
curl -H "X-Environment: dev" https://httpbin.org/headers
Copied!

Lesson 6: Send JSON in a POST request

Use -X POST, set content type, and pass data with -d.

curl -X POST https://httpbin.org/post \
  -H "Content-Type: application/json" \
  -d "{\"name\":\"Frank\",\"role\":\"student\"}"
Copied!

In Windows PowerShell, use curl.exe if curl maps to Invoke-WebRequest.

Lesson 7: Add bearer token auth

Most APIs use an Authorization header with a token.

curl -H "Authorization: Bearer YOUR_TOKEN_HERE" https://api.example.com/profile
Copied!

Lesson 8: Check just the HTTP status code

Useful for scripts and uptime checks.

curl -s -o NUL -w "%{http_code}\n" https://example.com
curl -s -o NUL -w "%{http_code}\n" https://httpbin.org/status/404
Copied!

Use /dev/null instead of NUL on Linux/macOS.

Lesson 9: Set timeout and retry

Prevent hanging requests and automatically retry transient failures.

curl --max-time 10 --retry 3 --retry-delay 2 https://api.example.com/health
Copied!

Lesson 10: Download and resume large files

Use -C - to resume interrupted downloads.

curl -L https://example.com/bigfile.iso -o bigfile.iso
curl -L -C - https://example.com/bigfile.iso -o bigfile.iso
Copied!

Advanced: Check many sites quickly (PowerShell loop)

Run curl across multiple URLs and print status codes in one report.

$urls = @(
  "https://example.com",
  "https://httpbin.org/status/200",
  "https://httpbin.org/status/503"
)

foreach ($url in $urls) {
  $code = curl.exe -s -o NUL -w "%{http_code}" $url
  [PSCustomObject]@{
    Url = $url
    StatusCode = $code
  }
}
Copied!

Quick reference table

Task Command
Simple GET curl https://example.com
Follow redirects curl -L https://example.com
Save to file curl -o file.txt https://example.com
Headers only curl -I https://example.com
POST JSON curl -X POST -H "Content-Type: application/json" -d "{\"name\":\"Frank\"}" https://httpbin.org/post
Status code only curl -s -o NUL -w "%{http_code}" https://example.com

Optional background

Why IT pros and coders use curl
  • Test APIs quickly without building a full app first.
  • Automate health checks, downloads, and integration tests.
  • Debug headers, auth problems, and redirect behavior from the terminal.

Related pages

Vim, PowerShell, and Git Cheatsheet

Reference sources used