Interview Prep/Shell Scripting/Troubleshooting: quoting, expansion and tool-input bugs

`curl -s "$API/status" | jq .state` fails with `jq: parse error: Invalid numeric literal at line 1, column 10`

IntermediateTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer

Likely causes → The response is not JSON: an HTML error page from a load balancer (<html>502 Bad Gateway), a plain-text error, or a redirect page. curl -s hides HTTP errors and still prints the body.

Diagnose →

curl -sS -o /tmp/body -w '%{http_code} %{content_type}\n' "$API/status"; head -c 300 /tmp/body

Fix → Fail on HTTP errors and check the status before parsing:

body=$(curl -fsS --max-time 10 "$API/status") || die "status API request failed"
state=$(jq -er '.state' <<<"$body") || die "unexpected response: ${body:0:200}"

Prevent → Always use curl -f (or check %{http_code}) in scripts, plus --max-time.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: quoting, expansion and tool-input bugs