1

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.

1
  • A friendly reminder: Googling 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 Commented Dec 13, 2016 at 8:14

2 Answers 2

4
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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

1 Comment

Oops, I forgot that. Added it now. Just insert it in the curl command line.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.