Log inSign up

Test web services from the shell with curl

curl ships with every supported image, so curl -I https://example.net already shows a site's status line and headers. Add -v to see the TLS handshake, -L to follow redirects, -d or --json to send data, and --resolve to test a new server before the DNS record points at it.

Topic: Networking and diagnostics · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Is curl already installed, and which version do you have?

On nearly every image curl is present from the start, because the package tools and many install scripts depend on it. Confirm it and note the version, since a few options below arrived in newer releases:

curl --version

If it is missing on a minimal Debian or Ubuntu install, add it with sudo apt install -y curl; on Rocky Linux 9 and AlmaLinux 9 use sudo dnf install -y curl. As a rough guide, Ubuntu 24.04 and Debian 13 ship curl 8.x, Debian 12 ships 7.88, while Ubuntu 22.04 (7.81) and Rocky and Alma 9 (7.76) are older. The --json shortcut needs 7.82 or later, so on the older ones use the longer form shown further down.

How do you look at status codes and headers?

The quickest health check is a HEAD request with -I, which prints only the response headers:

curl -I https://example.net

Some applications answer HEAD differently from GET. To see the headers of a real GET along with the body, use -i; to see the headers and discard the body, combine -s, -o /dev/null and -D -:

curl -i https://example.net/health
curl -s -o /dev/null -D - https://example.net/

When you only need the number, the write-out option prints chosen variables after the transfer:

curl -s -o /dev/null -w '%{http_code}\n' https://example.net/

That one line is ideal in scripts and cron checks: compare the output with 200 and alert on anything else.

How do you see what really happens during a request?

Verbose mode shows the address curl connected to, the TLS version and certificate details, every request header it sent and every response header it received. Lines beginning with > are what curl sent, < lines came back, and * lines are curl's own notes.

curl -v https://example.net/ -o /dev/null

Use -4 or -6 to force one address family, which quickly shows whether your AAAA record and the IPv6 listener both work:

curl -6 -I https://example.net
curl -4 -I https://example.net

If only the IPv6 attempt fails, check the web server listen lines and the firewall rules for IPv6, and see the IPv6 guide.

How do you follow redirects and handle certificates?

curl does not follow redirects unless asked. -L follows them and -I -L prints the header block of each hop, which is the easiest way to debug a redirect chain from HTTP to HTTPS and from the bare domain to www:

curl -sIL http://example.net | grep -iE '^(HTTP|location)'

Certificate errors are worth reading rather than bypassing. -k skips verification and is acceptable only for a quick look at a self signed test service; never place it in a script that sends secrets. To trust a private certificate authority properly, point curl at it:

curl --cacert /etc/ssl/private-ca.pem https://app.example.net/

The expiry date shows up in verbose output on the expire date line, which helps when you check that a renewal from certbot took effect.

How do you send POST requests and JSON?

-d sends form data with the POST method; repeat it or join fields with &. For JSON, set the content type and pass the body, or on curl 7.82 and newer use --json, which sets both content type and accept headers for you:

# form fields
curl -d 'user=deploy&note=hello' https://app.example.net/form

# JSON on any version
curl -H 'Content-Type: application/json' -d '{"name":"deploy","active":true}' https://app.example.net/api/users

# JSON shortcut, curl 7.82 or later
curl --json '{"name":"deploy"}' https://app.example.net/api/users

# body from a file
curl --json @payload.json https://app.example.net/api/users

Use -X PUT or -X DELETE for other methods. Add --fail-with-body in scripts so curl exits with an error on HTTP 400 and above while still printing the error message the API returned.

How do you pass credentials and headers safely?

Basic authentication goes in -u, bearer tokens and other values go in -H:

curl -u deploy https://app.example.net/admin/
curl -H "Authorization: Bearer $API_TOKEN" https://app.example.net/api/me

With -u deploy and no password, curl prompts for it, so it never lands in your shell history. Values typed on the command line are visible to other users through the process list while curl runs, so for scripts keep secrets in a file readable only by you and read headers from it with -H @/home/deploy/.api-headers, or store machine logins in ~/.netrc with mode 600 and add --netrc.

How do you test a new server before changing DNS?

When you migrate a site, you want to check the new server while the public record still points at the old one. --resolve pins a hostname to an address for that single command, and TLS still uses the real name, so certificates validate normally:

curl -I --resolve example.net:443:198.51.100.24 https://example.net/
curl -I --resolve example.net:443:[2001:db8:4f2a::24] https://example.net/

If the backend listens on a different port, --connect-to example.net:443:198.51.100.24:8443 redirects the connection while keeping the name. Once both answers look right, update the records as described in the DNS records tutorial. This pairs well with moving to a larger plan, where the new server gets a new address.

How do you download files and time requests?

-O saves under the remote file name, -o picks a name, -C - resumes a broken download and --retry 3 tries again on transient errors:

curl -fLO --retry 3 https://example.net/files/tool.tar.gz
curl -fL -C - -o tool.tar.gz https://example.net/files/tool.tar.gz

For slow pages, the write-out variables break a request into phases so you can see whether DNS, the TCP connect, TLS or the application is the slow part:

curl -s -o /dev/null -w 'dns %{time_namelookup}\nconnect %{time_connect}\ntls %{time_appconnect}\nfirst byte %{time_starttransfer}\ntotal %{time_total}\n' https://example.net/

Each value is seconds since the start, so subtract neighbours to get one phase. A large gap between TLS and first byte points at the application or database, not the network. Add --max-time 10 to any check that runs unattended so a hung server cannot block your script forever.

Questions about HTTP requests with curl

What is the difference between -I and -i?

-I sends a HEAD request and prints only headers. -i sends the normal request and prints headers followed by the body.

Why does curl print nothing for a redirect?

A 301 or 302 response often has an empty body. Add -L to follow it, or -I to see the location header.

Can curl test ports that are not HTTP?

Yes, for a simple reachability check: curl -v telnet://198.51.100.24:5432 reports whether the TCP connection opens. The port reachability guide lists other ways.

How do I make curl quiet in scripts but still show errors?

Use -sS: -s hides the progress meter and -S brings error messages back. Add -f or --fail-with-body so HTTP errors change the exit code.

Is wget a replacement?

wget is good for downloads and mirroring. curl covers more methods, headers and protocols, which suits API and web server testing better.