This lab covers curl and wget — the two primary command-line HTTP clients on Linux. You'll fetch web resources, inspect headers, make REST API calls, handle redirects, test SSL certificates, and measure request timing.
💡 curl supports 25+ protocols (HTTP, HTTPS, FTP, SFTP, etc.). The version output shows which libraries it was compiled with — OpenSSL for TLS, nghttp2 for HTTP/2, etc.
curl -I sends a HEAD request — fetches only headers, not the body.
💡 HTTP response headers tell you: server software, content type, cache policies, security headers (HSTS, CSP), and the response status code. A 200 OK means success; 301/302 = redirect; 404 = not found; 500 = server error.
📸 Verified Output:
Step 3: Silent Mode and JSON APIs
-s suppresses progress output; useful in scripts and when piping output.
💡 -H adds request headers. APIs often require headers like Authorization: Bearer TOKEN, Content-Type: application/json, or Accept: application/json. Always check API docs for required headers.
📸 Verified Output:
Step 4: Following Redirects with -L
By default, curl does NOT follow HTTP redirects. Use -L to follow them.
💡 -o /dev/null discards the body (we only want headers/metadata). -w writes formatted output after transfer. %{http_code} and %{url_effective} are curl write-out variables — there are 50+ available.
📸 Verified Output:
Step 5: POST Requests and Sending Data
💡 -X POST sets the HTTP method. -d sends data in the request body. For JSON APIs, always set -H "Content-Type: application/json". -u user:pass uses HTTP Basic Authentication (base64 encoded). Never use Basic Auth over plain HTTP.
📸 Verified Output:
Step 6: Downloading Files and Checking SSL
💡 wget --spider returns exit code 0 if the URL is reachable, non-zero otherwise — great for health-check scripts. curl -v in verbose mode shows TLS handshake details including certificate subject and expiry date.
📸 Verified Output:
Step 7: curl Timing Breakdown
curl's -w flag with timing variables reveals where time is spent in each request phase.
💡 Timing breakdown:namelookup = DNS resolution time; connect = TCP handshake; appconnect = TLS handshake (HTTPS only); pretransfer = ready to transfer; starttransfer = time to first byte (TTFB); total = everything. A slow namelookup indicates DNS issues; slow appconnect indicates TLS overhead.
📸 Verified Output:
Step 8: Capstone — REST API Client Script
Scenario: You need to write a shell script that queries a public REST API, checks HTTP status, and processes the JSON response. The script must handle errors gracefully.
💡 Always capture the HTTP status code separately from the body (-o FILE -w "%{http_code}"). Check for 2xx success before processing JSON. This pattern works for CI/CD health checks, API monitoring scripts, and automated testing pipelines.
# Without -L: stops at redirect
curl -I https://httpbin.org/redirect/1
# With -L: follows to final destination
curl -sL -o /dev/null -w '%{http_code} %{url_effective}\n' https://httpbin.org/redirect/1
# Show all redirect chain
curl -v -L -o /dev/null https://httpbin.org/redirect/3 2>&1 | grep -E "< HTTP|Location:"
200 https://httpbin.org/get
# POST with form data
curl -s -X POST -d "username=alice&password=secret" https://httpbin.org/post
# POST with JSON body
curl -s -X POST \
-H "Content-Type: application/json" \
-d '{"name":"Alice","role":"admin"}' \
https://httpbin.org/post | python3 -m json.tool
# POST with Basic Auth
curl -s -u "user:pass" https://httpbin.org/basic-auth/user/pass