← Blog · · 3 min read
curl: a 401 error page lands in your -o file and curl still exits 0
On this page (5 sections)
In our reproduction, a 4-part ranged download of a gated Hugging Face file produced a 524-byte "model": four 131-byte error pages, and every curl call exited 0. Without --fail, curl treats an HTTP 401 as a finished transfer and writes the error body into -o.
TL;DR Never run
curl -oin a script without-f(or--fail-with-body), and treat any non-zero exit as a failure: on curl 8.7.1,-fexited 56, not 22.
What happened
The first run of our parallel downloader hit a 429 rate limit. curl wrote the error pages into the parts, and the script concatenated them. We reproduced the pattern by fetching a gated model without a token, which returns 401.
Reproduce it
URL="https://huggingface.co/meta-llama/Llama-3.1-8B/resolve/main/model-00001-of-00004.safetensors"
ranges=("0-999" "1000-1999" "2000-2999" "3000-3999")
for i in 0 1 2 3; do
curl -sL -r "${ranges[$i]}" -o "part.$i" "$URL"
echo "part.$i: curl exit=$? size=$(wc -c < part.$i | tr -d ' ') bytes"
done
cat part.0 part.1 part.2 part.3 > model.bin
echo "model.bin: $(wc -c < model.bin | tr -d ' ') bytes (expected 4000)"
echo "first 120 bytes of part.0:"; head -c 120 part.0; echo
part.0: curl exit=0 size=131 bytes
part.1: curl exit=0 size=131 bytes
part.2: curl exit=0 size=131 bytes
part.3: curl exit=0 size=131 bytes
model.bin: 524 bytes (expected 4000)
first 120 bytes of part.0:
Access to model meta-llama/Llama-3.1-8B is restricted. You must have access to it and be authenticated to access it. Ple
| Variant | curl exit | What lands on disk |
|---|---|---|
-sL (no flag) |
0 | a 131-byte error page per part |
-fsSL |
56 | no file (part.0; loop aborted) |
--fail-with-body |
22 | the error text, in err.txt |
Why
By default, curl's exit code tells you whether the transfer worked, not whether the server agreed to send the file. A 401 response with a body is a complete transfer, so curl saves the body and returns 0.
The 56 is odd: --fail-with-body on the same URL returned 22. Our unconfirmed hypothesis is that this build reports a receive error when -f stops reading the response early, either in its TLS layer (SecureTransport) or its HTTP/2 path (nghttp2); we did not isolate which.
The fix
for i in 0 1 2 3; do
curl -fsSL -r "${ranges[$i]}" -o "part.$i" "$URL"
rc=$?
if [ -e "part.$i" ]; then sz=$(wc -c < "part.$i" | tr -d ' '); else sz="(no file)"; fi
echo "part.$i: curl exit=$rc size=$sz"
[ $rc -ne 0 ] && { echo "abort: part $i failed, not concatenating"; break; }
done
curl: (56) The requested URL returned error: 401
part.0: curl exit=56 size=(no file)
abort: part 0 failed, not concatenating
The check is $rc -ne 0; a check for 22 alone would have let this failure through. Our downloader now fails on HTTP errors and retries resumably from its own outer loop, without curl --retry.
Takeaways
- Give every
curl -oin a script-f, or--fail-with-bodyif you want to keep the server's message. - Check
$rc -ne 0, never 22 alone.
Applies to: curl 8.7.1 (SecureTransport) on macOS 27.0.1 with bash 3.2.57, against a Hugging Face 401. We did not test other curl builds, Linux, or a 429 in the reproduction.
Has curl -f ever exited with something other than 22 on your build?
How this was made: a short note drafted by an AI agent (Claude) from our own engineering logs; numbers are checked against those logs before publishing.