Batch 5: Protocols & Security Networking Apache & NGINX Docker ← Full TOC

← Protocols Index

🔗 REST APIs & curl — REST principles, curl, wget, API authentication patterns

REST (Representational State Transfer) is the dominant API architecture. Resources are identified by URLs, actions by HTTP methods. curl is the universal tool for testing APIs from the command line.

📋 Reference

ItemSyntax / ValueDescription
curl -Xcurl -X POST urlSpecify HTTP method
curl -Hcurl -H 'Content-Type: application/json'Set request header
curl -dcurl -d '{"key":"value"}'Set request body (implies POST)
curl --data-rawcurl --data-raw 'key=value&key2=value2'URL-encoded form data
curl -ocurl -o output.html https://example.com/Save response to file
curl -Ocurl -O https://example.com/file.tar.gzSave with remote filename
curl -Lcurl -L https://example.com/Follow redirects
curl -scurl -s urlSilent mode — no progress bar
curl -vcurl -v urlVerbose — show all headers
curl -Icurl -I urlHEAD request — headers only
curl -ucurl -u user:pass urlBasic authentication
curl -kcurl -k https://self-signed.local/Skip TLS certificate verification (insecure!)
curl --retrycurl --retry 3 --retry-delay 2 urlRetry on failure
curl -wcurl -w '%{http_code}' -o /dev/null -s urlGet HTTP status code only
curl -ccurl -c cookies.txt urlSave cookies to file
curl -bcurl -b cookies.txt urlSend cookies from file
curl --compressedcurl --compressed urlRequest gzip compression
curl -Fcurl -F 'file=@photo.jpg' -F 'name=Alice' urlMultipart form upload
curl --resolvecurl --resolve example.com:443:1.2.3.4 https://example.com/Override DNS for testing
wgetwget https://example.com/file.tar.gzDownload file
wget -rwget -r --no-parent https://example.com/docs/Recursive download
wget -qwget -q -O output.html urlQuiet mode, specify output file
wget --limit-ratewget --limit-rate=1m urlLimit download speed
REST: GET collectionGET /api/coursesList all courses
REST: GET itemGET /api/courses/42Get course with ID 42
REST: POSTPOST /api/courses {body}Create new course
REST: PUTPUT /api/courses/42 {body}Replace course 42
REST: PATCHPATCH /api/courses/42 {body}Update fields of course 42
REST: DELETEDELETE /api/courses/42Delete course 42
Bearer tokenAuthorization: Bearer eyJhbGci...JWT or OAuth2 token authentication
API key headerX-API-Key: myapikey123API key in custom header
Rate limitingRetry-After: 60 / X-RateLimit-Remaining: 42Headers indicating rate limit status
Pagination?page=2&per_page=20 / Link: <url>; rel=nextCommon API pagination patterns

💡 Example

# curl API reference — essential patterns

# ── REST API operations ───────────────────────────────────────
BASE="https://jsonplaceholder.typicode.com"

# GET — list resources
curl -s "$BASE/posts" | python3 -m json.tool | head -30

# GET — single resource
curl -s "$BASE/posts/1" | python3 -m json.tool

# GET with query params
curl -s "$BASE/posts?userId=1" | python3 -c 'import json,sys; posts=json.load(sys.stdin); print(f"{len(posts)} posts")'

# POST — create
curl -s -X POST "$BASE/posts" \
  -H "Content-Type: application/json" \
  -d '{"title":"MyWebUniversity","body":"Free education for all","userId":1}' \
  | python3 -m json.tool

# PUT — full replace
curl -s -X PUT "$BASE/posts/1" \
  -H "Content-Type: application/json" \
  -d '{"id":1,"title":"Updated Title","body":"Updated body","userId":1}' \
  | python3 -m json.tool

# PATCH — partial update
curl -s -X PATCH "$BASE/posts/1" \
  -H "Content-Type: application/json" \
  -d '{"title":"Just title changed"}' \
  | python3 -m json.tool

# DELETE
curl -s -X DELETE "$BASE/posts/1" -w "HTTP %{http_code}\n"

# ── Authentication patterns ───────────────────────────────────
# Bearer token (OAuth2 / JWT)
curl -s -H "Authorization: Bearer eyJhbGciOiJIUzI1NiJ9..." \
  https://api.example.com/profile

# API key in header
curl -s -H "X-API-Key: myapikey123" https://api.example.com/data

# API key in query param (less secure)
curl -s "https://api.example.com/data?api_key=myapikey123"

# Basic auth (username:password)
curl -s -u "alice:password123" https://api.example.com/admin/

# Get token first, then use it
TOKEN=$(curl -s -X POST https://api.example.com/auth/login \
  -H "Content-Type: application/json" \
  -d '{"username":"alice","password":"secret"}' \
  | python3 -c 'import json,sys; print(json.load(sys.stdin)["token"])')
curl -s -H "Authorization: Bearer $TOKEN" https://api.example.com/profile

# ── Useful curl patterns ──────────────────────────────────────
# Get only HTTP status code
curl -s -o /dev/null -w "%{http_code}" https://www.mywebuniversity.com/
# Output: 200

# Time the request
curl -s -o /dev/null -w "Connect: %{time_connect}s  Total: %{time_total}s" \
  https://www.mywebuniversity.com/

# Download and extract in one command
curl -L https://github.com/user/repo/archive/main.tar.gz | tar xz

# Upload file (multipart form)
curl -F "file=@report.pdf" -F "title=Annual Report" \
  https://api.example.com/upload

# Retry with exponential backoff
curl --retry 5 --retry-delay 2 --retry-max-time 60 \
  https://api.example.com/data

# Save cookies and send in next request
curl -c /tmp/cookies.txt -b /tmp/cookies.txt \
  -d "username=alice&password=secret" \
  https://example.com/login

# ── wget ─────────────────────────────────────────────────────
# Download single file
wget https://example.com/file.tar.gz

# Resume interrupted download
wget -c https://example.com/large-file.tar.gz

# Mirror a website section
wget -r -l 2 --no-parent -P ./mirror/ https://www.mywebuniversity.com/chapter-1.html

# Download quietly, save as specific filename
wget -q -O chapter42.html https://www.mywebuniversity.com/chapter-42.html

← SSH & SCP & SFTP  |  🏠 Index  |  Identity & Access Management →