HTTP client status and timeouts
HTTP automation has two distinct result channels: the client exit status tells you whether the transfer succeeded according to its options, while the HTTP status describes the server response. A server may return 404 and curl may still exit zero. A connection failure or timeout is a different event. Never treat a successful TCP transfer as successful business processing.
Define the request contract
A request contains a method, URL, headers and sometimes a body. A response contains a status, headers and usually a body. Write body bytes into a private file using -o, and use -w '%{http_code}' for the status. Capture the curl status immediately; use -sS to hide progress while preserving errors. --connect-timeout bounds connection establishment (including protocol negotiation as applicable); --max-time bounds the individual transfer, not an entire multi-request workflow. --fail-with-body (curl 7.76.0+) returns an error for HTTP failures while retaining the response body. It is not a substitute for validating an expected success code or the body's schema. Redirects are not automatically followed by these fixtures.
The following examples use curl 8.14.1 and the supplied loopback mock server. The URL guard, disabled default curl config (-q first), explicit no-proxy and HTTP-only protocol are lab-specific controls. Use HTTPS with certificate and hostname verification for real remote services. For query parameters, use --get --data-urlencode instead of constructing a URL from raw text; encoding is not authorization or validation. Do not mix status strings into the JSON body. Use a fresh private workspace and restrictive umask before creating body or diagnostic files. Check each body read before reporting the next response. The timeout drill checks the diagnostic and status, not a particular elapsed wall-clock time.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. HTTP examples require the included lab-fixtures/v25/mock_api.py server: run python3 on that file in a separate terminal, export BM_API_BASE with its printed loopback URL, and stop the server with Ctrl-C when done. Use a fresh server for each HTTP example. curl and jq are external dependencies.
Separate status from response body
umask 077[[ ${BM_API_BASE-} =~ ^http://127\.0\.0\.1:[0-9]{1,5}$ ]] || exit 2for route in ok missing; do code=$(curl -q --noproxy "*" --proto "=http" -sS --connect-timeout 1 --max-time 2 -o body.json -w '%{http_code}' "$BM_API_BASE/$route") rc=$? (( rc == 0 )) || exit "$rc" printf 'curl=%s http=%s\n' "$rc" "$code" cat body.json || exit 1doneExpected stdout:
curl=0 http=200{"ok":true}curl=0 http=404{"error":"not found"}The transport succeeds in both cases. Only the first matches an HTTP 200 contract.
Classify an overall timeout
umask 077[[ ${BM_API_BASE-} =~ ^http://127\.0\.0\.1:[0-9]{1,5}$ ]] || exit 2code=$(curl -q --noproxy "*" --proto '=http' -sS --connect-timeout 1 --max-time 0.2 -o body.json -w '%{http_code}' "$BM_API_BASE/slow" 2> diagnostic.txt)rc=$?printf 'curl=%s http=%s\n' "$rc" "$code"[[ $rc == 28 && -s diagnostic.txt ]] || exit 1Expected stdout:
curl=28 http=000The mock delays its response; curl reports timeout 28 and no received HTTP status. 000 is a client placeholder, not a server status.
Encode a query value
[[ ${BM_API_BASE-} =~ ^http://127\.0\.0\.1:[0-9]{1,5}$ ]] || exit 2curl -q --noproxy "*" --proto "=http" -sS --connect-timeout 1 --max-time 2 --fail --get --data-urlencode 'q=daily report' "$BM_API_BASE/search"Expected stdout:
{"query":"daily report"}The fixture accepts encoded spaces. curl encodes the query value rather than interpreting it as shell syntax.
Common mistakes
A 404 without --fail can return zero; --max-time is not a global workflow deadline. Never assume every 2xx body satisfies your application contract.
Practice
Start the included mock server, compare /ok and /missing, then classify a timeout. Explain the client and HTTP statuses separately; use no external URL.
Next: Structured JSON with jq