1

I have a curl call:

curl --silent --connect-timeout 8 --output /dev/null http://0.0.0.0:5000/twi?c0=24 -I -w "%{http_code}\n" ``

It works fine and returns 200 if succeeded. I want the status code in a variable and tried:

curl --silent --connect-timeout 5 --output /dev/null http://0.0.0.0:5000/twi?c0=24

And then:

curl_status=$?
echo $curl_status

This call works well but gives 0 as $curl_status.

I tried:

curl_status=$(curl --silent --connect-timeout 5 --output /dev/null http://0.0.0.0:5000/twi?c0=24)
echo $curl_status

With the same result: call is executed, but gives 0 as $url_status.

Why do I not get the http_code of the first call into a variable to use in an if statement? I read many posts here with no success for a solution.

3
  • 4
    superuser.com/a/442395/59795 Commented Jun 28, 2018 at 7:59
  • Exit code ≠ HTTP status code. Commented Jun 28, 2018 at 8:00
  • Your expectation that curl uses the http status as exit code is just wrong. Read the manual if you are unsure if, it is meant for that: man curl. Commented Jun 28, 2018 at 12:17

2 Answers 2

2

Try

curl_status=`curl --silent --connect-timeout 8 --output /dev/null http://0.0.0.0:5000/twi?c0=24 -I -w "%{http_code}\n"`

If you want to follow redirects use -L

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

Comments

1

With help of the link from Biffen and the example from https://coderwall.com/p/taqiyg/use-http-status-codes-from-curl I came to

curl_status=$(curl -s -o /dev/null -w '%{http_code}' --connect-timeout 3 http://0.0.0.0:5000/twi?c0=24+91+24+91+24+91+24+91)``

Thanks for the help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.