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 workedIf 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
HardShow that a 404 is still a successful request.
- Serve a folder on port 9120
- Ask for a page that does not exist and print
status=with the status code - On the next line print
exit=with curl's exit code
Expected output:
status=404
exit=0Read
$?immediately after the curl that produced it.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Networking with the Terminal
6curl in Depth
Seeing the HeadersSaving ResponsesCustom HeadersStatus and Timingcurl Exit CodesRecap: curl FlagsPractice on your own: Terminal playground