Skip to main content

Fixed: "curl: (56) The requested URL returned error: 404" with curl --fail

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Terminal
curl -f https://httpbin.org/status/404; echo $?

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​

Output
curl: (56) The requested URL returned error: 404
56

The terminal: curl -f to httpbin.org/status/404 printed curl: (56) The requested URL returned error: 404, and the exit code 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:

    Terminal
    curl --version
    Output (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.1
    Release-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:

    Output
    curl: (22) The requested URL returned error: 404
    22
  • curl's issue tracker. Issue #13411, "HTTP/2 + CURLOPT_FAILONERROR returns incorrect error code CURLE_RECV_ERROR", describes exactly this. CURLOPT_FAILONERROR is what --fail sets; libcurl's list gives it "CURLE_HTTP_RETURNED_ERROR (22)". curl's lead developer reproduced the bug with curl -f over 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:

Terminal
curl -fsS https://httpbin.org/status/404 > /dev/null || echo "The request failed."

If you need 22 exactly, use curl 8.8.0 or later, or add --http1.1.

After the Fix​

Output
curl: (56) The requested URL returned error: 404
The request failed.

The terminal after the fix: curl reported the 404 and the shell printed The request failed.

The failure was caught, whatever code curl used. PowerShell 7 printed the same two lines.

Common Mistakes​

MistakeWhat happensDo this instead
if [ $? -eq 22 ] after curl -fMisses the failure on curl 8.7.1 over HTTP/2, which exits with 56Test for non-zero, or use ||
Treating this 56 as a network problem56 normally means a receive failure, but here the message names the HTTP errorRead the message, not just the code

Troubleshooting​

SymptomLikely causeFix
curl: (56) The requested URL returned error: 404curl 8.7.1, --fail and HTTP/2Check 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​