gpt4 book ai didi

linux - 如何在linux中使用curl分别获取http状态码和内容

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:46:56 24 4
gpt4 key购买 nike

我必须使用 curl linux 实用程序获取一些数据。有两种情况,一种请求成功,第二种不成功。如果请求成功,我想将输出保存到一个文件中,如果请求由于某种原因失败,那么错误代码应该只保存到一个日志文件中。我在 www 上搜索了很多但找不到确切的解决方案,这就是为什么我在 curl 上发布了一个新问题。

最佳答案

一种选择是使用-w 获取响应代码,因此您可以这样做

code=$(curl -s -o file -w '%{response_code}' http://example.com/)
if test "$code" != "200"; then
echo $code >> response-log
else
echo "wohoo 'file' is fine"
fi

关于linux - 如何在linux中使用curl分别获取http状态码和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41114306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com