luffy008
luffy008

Reputation: 37

How to get the error summary separately from a response body using curl in shell script

I am trying to get the status code and exit status from the response of an api request using curl in shell script.

status_code=$(curl --write-out %{http_code} --silent --output /dev/null -X POST http://..............)
exit_status="$?"
echo "$status_code"
echo "$exit_status"

I am getting both the things.But now i also want the error summary(if its there any) from the reponse and to be printed.How can i do that?

Below is the example of my response code

{
    "errorCode": "E0000014",
    "errorSummary": "Update of credentials failed",
    "errorLink": "E0000014",
    "errorId": "oaeKE7Weyp7RsKYDNR2V0Sw9w",
    "errorCauses": [
        {
            "errorSummary": "Old Password is not correct"
        }
    ]
}

I want to get the errorSummary inside the errorCauses i.e. I want to print the "Old Password is not Correct" string(Or whatever the strings come). I have tried but couldn't find any solution for it

Upvotes: 0

Views: 131

Answers (1)

dash-o
dash-o

Reputation: 14452

Change curl to store the response document (instead of sending it to /dev/null), and use jq to extract the first components of the errorCauses array.

curl ... -output response.txt -X ...
exit_status="$?"
...
jq '.errorCauses[0].errorSummary' < response.txt

Upvotes: 1

Related Questions