【问题标题】:Can I have curl print just the response code?我可以让 curl 只打印响应代码吗?
【发布时间】:2021-04-14 12:57:48
【问题描述】:

我读了https://superuser.com/questions/272265/getting-curl-to-output-http-status-code。它提到了

curl -i 

将打印 HTTP 响应代码。是否可以让 curl just 打印 HTTP 响应代码?有没有一种通用的方法来获取任何类型的请求(如 GET/POST/etc)的 HTTP 状态代码?

我在 Mac OS High Sierra 上使用 curl 7.54.0。

感谢阅读。

【问题讨论】:

标签: http curl


【解决方案1】:

这对我有用:

$  curl -s -w "%{http_code}\n" http://google.com/ -o /dev/null

【讨论】:

    【解决方案2】:
    curl -s -I http://example.org | grep HTTP/ | awk {'print $2'}
    

    输出:200

    【讨论】:

    • 谢谢。我阅读了 curl 手册页。 -我将发送 HEAD 请求。有没有一种通用的方法来获取 GET/POST/etc 的 HTTP 状态码?
    • 但这种情况很少见,通常使用专用的命令行选项调用,例如--upload-file for PUT
    猜你喜欢
    • 1970-01-01
    • 2011-08-01
    • 2020-12-20
    • 1970-01-01
    • 2017-08-20
    • 2021-01-25
    • 1970-01-01
    • 2014-09-21
    • 2022-12-07
    相关资源
    最近更新 更多