Lab 12: Networking — curl, wget & HTTP

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm ubuntu:22.04 bash


Overview

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.


Step 1: Install Tools and Basic curl Usage

apt-get update -qq && apt-get install -y curl wget
curl --version

💡 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.

📸 Verified Output:

curl 7.81.0 (x86_64-pc-linux-gnu) libcurl/7.81.0 OpenSSL/3.0.2 zlib/1.2.11 
brotli/1.0.9 zstd/1.4.8 libidn2/2.3.2 libpsl/0.21.0 (+libidn2/2.3.2) 
libssh/0.9.6/openssl/zlib nghttp2/1.43.0 librtmp/2.3 OpenLDAP/2.5.20
Release-Date: 2022-01-05

Step 2: HTTP Headers with curl -I

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.

📸 Verified Output:


Summary

Tool / Flag
Purpose

curl -I URL

Fetch headers only (HEAD request)

curl -s URL

Silent mode (suppress progress)

curl -L URL

Follow HTTP redirects

curl -o FILE URL

Save response to file

curl -X POST -d DATA URL

Send POST with body data

curl -H "Key: Value" URL

Add custom request header

curl -u user:pass URL

HTTP Basic Authentication

curl -w "fmt" URL

Write-out formatted metadata

curl -v URL

Verbose: show full TLS/HTTP exchange

wget --quiet URL

Download silently

wget --output-document=FILE URL

Download to specific filename

wget --spider URL

Check URL exists (exit code only)

HTTP 200

OK — success

HTTP 301/302

Redirect — use -L to follow

HTTP 401/403

Auth required / forbidden

HTTP 404

Not found

HTTP 500

Server error

Last updated