When testing some REST API, I'm using a cURL command.
However when the request failed, it seems that cURL does not indicate that there was a non-success status code (HTTP/2 405
in my case); instead I just see the response (which is a JSON "messages" array).
As it would require "quite a lot of AI" to deduce from the contents of the message array that there was an error, I wonder how I can reliably detect the non-success status code when using cURL.
Maybe it could even be a bug in cURL (regarding use of HTTP/2).
Example session (I deliberately mad a request error to demonstrate):
> curl -u monitor 'https://redacted.server.name/api/status/get-token'
Enter host password for user 'monitor':
{
"messages": [
"The global command get-token requires to use PUT"
]
}
> echo $?
0
curl --write-out '\nresponse_code=%{response_code}\n' 'https://redacted.server.name/api/status/get-token'
should end with
response_code=405
if you need it in a variable i suppose you could do
RESPONSE=$(curl --write-out '\n%{response_code}' 'https://redacted.server.name/api/status/get-token')
RESPONSE_CODE=$(tail -1 <<<$RESPONSE)
RESPONSE=$(echo "$RESPONSE" | head --lines=-1)
now echo "$RESPONSE_CODE"
should give you 405
and echo "$RESPONSE"
should give you the original http response body...
Btw seems you're approaching the complexity where using bash no longer makes sense, maybe try switching to a better scripting language like PHP, Python, or Perl? They all have good libcurl bindings.