curl exits 0 when the server says 404
A 404 is not a curl failure — the request was made, a response came back, and curl reports success. With -o it writes the error page into your output file, over whatever was there before, and the script that called it has no way to tell the difference.
The symptom
$ curl -s -o dl.json "https://api.github.com/repos/octocat/this-repo-does-not-exist-xyz1234"
$ echo $?
0
$ wc -c < dl.json
132
Exit 0 and a 132-byte file. A script checking if curl ...; then takes the success branch, and a script checking [ -s dl.json ] finds a non-empty file. Both are satisfied. What is in the file:
$ head -c 200 dl.json
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/repos/repos#get-a-repository",
"status": "404"
}
Nothing was downloaded. An error message was downloaded.
This is not a bug, which is why it survives
curl reports on the transport, not on the application. DNS resolved, the connection opened, TLS completed, a well-formed HTTP response arrived in full. By every measure curl is responsible for, that is a success — and the response body is the body it was given, so writing it to -o is the correct thing to do with it.
The mistake is in reading "curl exited 0" as "I have the thing I asked for".
The damage: it overwrites the copy you had
This is the part worth being alarmed about. The same request, aimed at a file that already contained good data:
$ echo '{"good":"data from yesterday"}' > cache.json
$ cat cache.json
{"good":"data from yesterday"}
$ curl -s -o cache.json "https://api.github.com/repos/octocat/this-repo-does-not-exist-xyz1234"
$ echo $?
0
$ cat cache.json
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/repos/repos#get-a-repository",
"status": "404"
}
-o truncates its target before it knows what the response will be. A nightly refresh that hits a renamed endpoint does not leave yesterday's data in place — it replaces it with an error object, and exits 0 while doing so. The next run of whatever consumes cache.json is now parsing an error message as data.
Running it by hand shows you the problem; running it in a script hides it. Interactively you omit -o, the body prints to your terminal, and you read "Not Found" immediately. In a script the body goes into a file nobody looks at, and the only signal left is an exit status that says success.
Measured, the interactive form:
$ curl -s "https://api.github.com/repos/octocat/this-repo-does-not-exist-xyz1234"
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/repos/repos#get-a-repository",
"status": "404"
}
$ echo $?
0
Same exit 0. The difference is entirely in whether a human happens to be looking at stdout.
The fixes, measured, with the control
--fail: nonzero exit, and no file
$ curl -s --fail -o dl2.json "https://api.github.com/repos/octocat/this-repo-does-not-exist-xyz1234"
$ echo $?
22
$ test -f dl2.json && echo yes || echo no
no
Exit 22, and the output file was never created — so a downstream [ -f ] check now also tells the truth. The cost is that you lose the server's explanation, which is often the only thing that tells you *why*.
--fail-with-body: nonzero exit and the message
$ curl -s --fail-with-body -o b.json "https://api.github.com/repos/octocat/this-repo-does-not-exist-xyz1234"
$ echo $?
22
$ cat b.json
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/repos/repos#get-a-repository",
"status": "404"
}
Exit 22 with the body kept. This is almost always what you actually wanted: fail loudly, and still be able to read the reason. Available since curl 7.76.
Or ask for the status code directly
$ curl -s -o /dev/null -w "http_code=%{http_code}\n" "https://api.github.com/repos/octocat/this-repo-does-not-exist-xyz1234"
http_code=404
One request, and the number you actually care about. Useful when you want to branch on *which* failure it was rather than merely that there was one.
The control: does --fail break the success case?
A flag that always fails is not a fix. The same two commands against a URL that works:
plain exit=0 bytes=5591
--fail exit=0 bytes=5591
Identical. --fail exits 0 and writes the same 5591 bytes on a 200, so the 22 above is a real discrimination between a good response and a bad one, not a flag that reports failure unconditionally.
What each check actually tells you
| check | on a 200 | on a 404 | useful? |
|---|---|---|---|
curl -s -o f, then $? | 0 | 0 | no |
[ -f f ] after -o | true | true | no |
[ -s f ] after -o | true | true | no |
curl --fail, then $? | 0 | 22 | yes |
curl --fail-with-body | 0 | 22, body kept | yes |
-w '%{http_code}' | 200 | 404 | yes |
The top three are the checks most scripts actually contain.
Why this one is worth a page
- The failure mode is a successful file. Not a missing file, not an empty
file — a plausible, non-empty, well-formed file with the wrong contents.
- It destroys the previous good copy on the way.
-otruncates first and
asks questions later.
- The interactive form is self-diagnosing and the scripted form is not.
The habit of checking a command by hand before putting it in a script is exactly what fails to transfer here.
- Every tool in the chain is behaving correctly.
curlreported on the
transport; the server reported the resource was missing; the shell reported curl's exit status. Nobody lied.
The general form: find out which layer a tool's success refers to. curl answers "did the HTTP transaction complete". Whether the answer was the one you wanted is a different question, asked with a different flag.
Measured on
Windows 11, Git Bash MSYSTEM=MINGW64, curl 8.18.0 (x86_64-w64-mingw32) libcurl/8.18.0 Schannel, 2026-11-28. Requests are to api.github.com, a public documented API, well inside its unauthenticated rate limit: one request for a repository that does not exist, and one for octocat/Spoon-Knife, which does. --fail-with-body requires curl 7.76 or newer; the behaviour of older curl was not tested, because only 8.18.0 is installed here. Byte counts are live values from that day and will drift.