Menu
Coddy logo textTech

curl Exit Codes

Lesson 30 of 47 in Coddy's Networking with the Terminal course.

There are two completely different notions of failure in one curl command, and mixing them up causes real bugs.

The status code is the server's answer: 200, 404, 500. The exit code is whether curl managed to have the conversation at all.

A 404 is a successful conversation. The server was reached, understood the request and answered. curl exits 0. A script that only checks the exit code will treat that as fine, which is a classic source of pipelines that pass while downloading an error page.

curl -s -o /dev/null http://127.0.0.1:9120/missing
echo $?     # 0, because the request itself worked

If you want failure on a bad status, ask for it with --fail, which makes curl exit non-zero on 4xx and 5xx. Otherwise check %{http_code} yourself.

challenge icon

Challenge

Hard

Show that a 404 is still a successful request.

  1. Serve a folder on port 9120
  2. Ask for a page that does not exist and print status= with the status code
  3. On the next line print exit= with curl's exit code

Expected output:

status=404
exit=0

Read $? immediately after the curl that produced it.

Try it yourself

Terminal
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Networking with the Terminal

Practice on your own: Terminal playground