linux中如何使用curl分别获取http状态码和内容
How to get http status code and content separately using curl in linux
我必须使用 curl linux
实用程序获取一些数据。有两种情况,一种请求成功,第二种不成功。如果请求成功,我想将输出保存到一个文件中,如果请求由于某种原因失败,那么错误代码应该只保存到一个日志文件中。我在 www 上搜索了很多但找不到确切的解决方案,这就是为什么我在 curl 上发布了一个新问题。
curl -I -s -L <Your URL here> | grep "HTTP/1.1"
curl + grep 是你的朋友,那么你可以稍后根据需要提取状态码。
一个选项是使用 -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
我必须使用 curl linux
实用程序获取一些数据。有两种情况,一种请求成功,第二种不成功。如果请求成功,我想将输出保存到一个文件中,如果请求由于某种原因失败,那么错误代码应该只保存到一个日志文件中。我在 www 上搜索了很多但找不到确切的解决方案,这就是为什么我在 curl 上发布了一个新问题。
curl -I -s -L <Your URL here> | grep "HTTP/1.1"
curl + grep 是你的朋友,那么你可以稍后根据需要提取状态码。
一个选项是使用 -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