如何在shell脚本中使用curl从响应体中单独获取错误摘要



我正试图在shell脚本中使用curl从api请求的响应中获取状态代码和退出状态。

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

两样东西我都得到了。但现在我也想从回复中得到错误摘要(如果有的话(并打印出来。我该怎么做?

下面是我的响应代码的示例

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

我想在errorCauses中获得errorSummary,即我想打印"Old Password is not Correct"字符串(或任何字符串(。我试过了,但找不到任何解决方案

更改curl以存储响应文档(而不是将其发送到/dev/null(,并使用jq提取errorCauses数组的第一个组件。

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

相关内容

最新更新