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.
2 Answers
curl -I -s -L <Your URL here> | grep "HTTP/1.1"
curl + grep is your friend, then you can extract the status code later for your need.
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
-
Oops, I forgot that. Added it now. Just insert it in the curl command line. Commented Dec 13, 2016 at 8:26
How to get http status code and content seprately using curl in linux
should already give you everything you need; always remember to do that first