Fixed: "curl: (56) The requested URL returned error: 404" with curl --fail
I checked curl's documented exit code 22 after curl --fail and got 56 instead: curl: (56) The requested URL returned error: 404. The cause is a known bug in curl 8.7.1, the version macOS ships, which curl fixed in 8.8.0. A check for any non-zero exit code works on every version.
What I Was Doing
I was writing a guide to curl -f, which makes curl fail on HTTP errors, and checked its exit code for a page that answers 404 Not Found, on macOS with the curl that comes with it. $? holds the exit code in zsh and bash, $LASTEXITCODE in PowerShell:
- macOS / Linux
- Windows (PowerShell)
curl -f https://httpbin.org/status/404; echo $?
curl -f https://httpbin.org/status/404; $LASTEXITCODE
What I Expected
Exit code 22. The man page for --fail: "Fail with error code 22 and with no response body output at all for HTTP transfers returning HTTP response codes at 400 or greater."
What I Got
curl: (56) The requested URL returned error: 404
56

The message names the 404 correctly, but the code is 56. libcurl's error list: "CURLE_RECV_ERROR (56) Failure with receiving network data." PowerShell 7, running the same curl, also printed 56.
What I Checked
-
The curl version:
Terminalcurl --versionOutput (first two lines)curl 8.7.1 (x86_64-apple-darwin25.0) libcurl/8.7.1 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.12 nghttp2/1.68.1Release-Date: 2024-03-27 -
The protocol. The response came over HTTP/2. With
--http1.1, the same request gave the documented 22, in zsh and in PowerShell:Outputcurl: (22) The requested URL returned error: 40422 -
curl's issue tracker. Issue #13411, "HTTP/2 + CURLOPT_FAILONERROR returns incorrect error code CURLE_RECV_ERROR", describes exactly this.
CURLOPT_FAILONERRORis what--failsets; libcurl's list gives it "CURLE_HTTP_RETURNED_ERROR (22)". curl's lead developer reproduced the bug withcurl -fover HTTP/2 against--http1.1. -
curl's changelog. The fix is listed under "Fixed in 8.8.0 - May 22 2024", linked to that issue: "http2 + ngtcp2: pass CURLcode errors from callbacks".
Why It Happens
The issue traces it: when --fail stops an HTTP/2 transfer, curl's HTTP/2 code turns the original error into a generic callback failure, which comes out as a receive error, 56. HTTP/1.1 doesn't go through that code and keeps 22. macOS ships curl 8.7.1, from before the fix.
The Fix
Don't depend on the exact code 22. Check whether curl failed at all, which works on every version and protocol. || runs the second command only when curl fails:
- macOS / Linux
- Windows (PowerShell)
curl -fsS https://httpbin.org/status/404 > /dev/null || echo "The request failed."
curl -fsS https://httpbin.org/status/404 > $null || Write-Output "The request failed."
If you need 22 exactly, use curl 8.8.0 or later, or add --http1.1.
After the Fix
curl: (56) The requested URL returned error: 404
The request failed.

The failure was caught, whatever code curl used. PowerShell 7 printed the same two lines.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
if [ $? -eq 22 ] after curl -f | Misses the failure on curl 8.7.1 over HTTP/2, which exits with 56 | Test for non-zero, or use || |
| Treating this 56 as a network problem | 56 normally means a receive failure, but here the message names the HTTP error | Read the message, not just the code |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
curl: (56) The requested URL returned error: 404 | curl 8.7.1, --fail and HTTP/2 | Check for any non-zero code, or use curl 8.8.0 or later |
FAQ
Is my curl affected?
Run curl --version. Version 8.7.1, released 2024-03-27, was; curl lists the fix under 8.8.0.
Does --fail-with-body have the same problem?
Not in my test: curl --fail-with-body on a missing page of curl.se, over HTTP/2, exited with 22.
Conclusion
curl --fail on curl 8.7.1 reported a 404 over HTTP/2 with exit code 56, a bug curl fixed in 8.8.0. A check for any non-zero exit code caught the failure, and --http1.1 gave the documented 22.
Additional Resources
- curl man page: -f, --fail — the documented behaviour.
- curl issue #13411 — the bug report.
- curl changelog — the fix in 8.8.0.
- libcurl error codes — what 22 and 56 mean.
