简体   繁体   中英

How to get http status code and content separately using curl in linux

I have to fetch some data using curl linux utility. There are two cases, one request is successful and second it is not. I want to save output to a file if request is successful and if request is failed due to some reason then error code should be saved only to a log file. I have search a lot on www but could not found exact solution that's why I have posted a new question on curl.

curl -I -s -L <Your URL here> | grep "HTTP/1.1"

curl + grep是您的朋友,然后您可以根据需要提取状态代码。

One option is to get the response code with -w, so you could do it something like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM