如何从 cURL 8.0 获取 HTTP 状态错误?

问题描述 投票:0回答:1

在测试某些 REST API 时,我使用 cURL 命令。 然而,当请求失败时,cURL似乎并没有表明存在非成功状态代码(在我的例子中是

HTTP/2 405
);相反,我只看到响应(这是一个 JSON“消息”数组)。

由于需要“相当多的人工智能”才能从消息数组的内容中推断出存在错误,因此我想知道在使用 cURL 时如何可靠地检测到非成功状态代码。

也许它甚至可能是 cURL 中的一个错误(关于 HTTP/2 的使用)。

示例会话(我故意发了一个请求错误来演示):

> 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
http curl http-status-codes
1个回答
0
投票
curl --write-out 'response_code=%{response_code}\n' 'https://redacted.server.name/api/status/get-token'

应该以

结尾
response_code=405
© www.soinside.com 2019 - 2024. All rights reserved.