Why curl Exits Successfully on HTTP 404: Fail CI and Keep Error Bodies with --fail-with-body
Separate curl exit codes from HTTP status, compare failure options, and verify a 2xx-only CI script against local 200, 302, and 404 responses.
To fail a CI step on HTTP 404 while retaining the server's error body, use curl's --fail-with-body and inspect the process exit code separately from the HTTP status. Default curl can exit with code 0 after successfully transferring a 404 page. This article reproduces the option differences with a local Linux HTTP server and verifies a shell script that accepts only 2xx responses.
AI-generated conceptual illustration of separating connection success from an error response while retaining its body. This is not an exact network flowchart.
Written and documentation checked on 2026-11-24. The tested environment was Linux x86_64, curl 8.14.1, and Python 3.12.14. --fail-with-body was introduced in curl 7.76.0. Official option history
HTTP 404 and process exit code 22 are different values
Curl's default success criterion is different from application-level HTTP success. If a server successfully transfers a 404 response and error body, the transfer itself can succeed. A subsequent command chained to default curl's success can therefore run unexpectedly. The curl project's HTTP response explanation
The HTTP status comes from the server; the exit code is the curl process's result. Do not mix them by returning HTTP 404 as exit 404 in a shell. In the comparison below, 22 is curl's exit code when HTTP errors are configured to count as failures. Curl exit codes
| Option used for the local 404 response | Curl exit code | Error body saved |
| Default behavior | 0 | Yes |
--fail | 22 | No |
--fail-with-body | 22 | Yes |
The table reports actual results from the reproduction below. Handle body preservation together with the failure result so diagnostic information is not lost.
A CI script that separates body, status, and exit code
Save this as curl_ci.sh. Its first argument is the URL and its second is the response-body output file. The specified output file can be overwritten, so use a fresh path for each request.
--fail-with-body is the alternative that retains the body on HTTP errors; do not combine it with --fail. Official curl option documentation
The body goes to the --output file, while the HTTP status goes through --write-out into code. Placing -q first excludes the default curl configuration file. The example chooses connection and overall time limits of 2 and 5 seconds. --silent --show-error reduces progress output while retaining error messages. Curl manual
On a curl failure, the script captures $? immediately inside else and returns the original exit code. Even after curl succeeds, it checks for a 2xx status and otherwise returns the script's own failure code 1. Consequently, 302 fails this policy. Redirects are not followed automatically: the example chooses not to silently accept a redirect to a login page instead of the expected API.
Verify eight paths with a local server
Save this as curl_exit_demo.py in the same directory and run python3 curl_exit_demo.py. It needs only the Python standard library and curl. The server uses an ephemeral loopback port and calls no external service.
The actual output was:
Assertions compare the exit code, HTTP status, and exact saved body bytes. body=True indicates a body existed in this reproduction; it does not mean every 2xx API response has a body.
The final path in the five option comparisons confirms that --fail-with-body alone does not fail on 302. The next three paths verify that the CI script preserves the 404 error body and returns 22, returns 0 for 200, and returns 1 for 302 under its separate policy. The test program removes its temporary body files on exit; running the CI script independently leaves the specified file in place.
Scope when applying this to deployment checks
This script checks HTTP status. It does not validate application error fields in a 200 body, the expected JSON structure, or the validity of a downloaded file. Add separate body validation after HTTP success when required. If 404 is a normal lookup result for your API, change the 2xx-only policy first.
DNS, TLS, connection failures, and timeouts can leave no HTTP response or only a partial body. Do not treat an existing file on a failure path as a complete error response, and do not reuse a previous request's output file. Error bodies can contain sensitive information; retain only what is needed instead of automatically printing the entire body into CI logs.
Verification here covers local 200, 302, and 404 responses and shell exit-code propagation. Real DNS, TLS, authentication negotiation, proxies, network timeouts, and CI-platform artifact retention were not tested. Connection-count or execution-time performance figures were not measured either.
한국어 버전:
curl이 HTTP 404에도 성공할 때: --fail-with-body로 CI 실패 감지와 오류 본문 보존하기