Skip to content
shellhacks.dev
APIs

Send a GET request and show the status code

Print only the HTTP status code of a request with curl -s -o /dev/null -w '%{http_code}'. The exit status stays 0 for a 404 unless you add --fail.

1 min read
Summarize in AI

A health check needs the status code of a URL, not the body, and a script wants it on a line of its own.

Shell
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/index.txt
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1:8000/missing
curl -s -o /dev/null http://127.0.0.1:8000/missing; echo "exit: $?"
curl -s -f -o /dev/null http://127.0.0.1:8000/missing; echo "exit with -f: $?"
Text
200
404
exit: 0
exit with -f: 22

-w prints a format after the transfer, and %{http_code} is one of the variables listed on the curl man page. With -o /dev/null the body is discarded and -s hides the progress meter, so the code is all that is printed. A 404 is still a completed transfer for curl, so the exit status stays 0 until -f turns an HTTP error into exit 22.

The code reported is the last one, after any redirect you asked curl to follow with -L. Add %{url_effective} to see where it ended. A refused connection prints 000 as the code, so treat 000 as "no response", not as a status.

Share

More snippets

APIs

Show only the response headers

Show only the response headers with curl, either by sending a HEAD request or by dumping the headers of a real GET with -D - and dropping the body.

1 min read
APIs

Save a download with its remote name

Save a download under the name in the URL with curl -O. It writes to the current directory, and a URL that ends in a slash has no name to use.

1 min read
APIs

Send a custom header

Send a custom request header with curl -H 'Name: value'. A header with an empty value after a colon removes one of curl's defaults, such as User-Agent.

1 min read

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.