Get page output with curl --fail Get page output with curl --fail curl curl

Get page output with curl --fail


First of all the maximum value for the error code(or exit code) is 255. Here is the reference.

Also, the --fail will not allow you to do what you are looking for. However, you can use alternate ways(writing a shell script) to handle the scenario, but not sure it will be effective or not for you!

http_code=$(curl -s -o out.html -w '%{http_code}'  http://www.google.com/linux;)if [[ $http_code -eq 200 ]]; then    exit 0fi## decide which status you want to return for 404 or 500exit  204

Now do the $? and you'll get the exit code from there.

You'll find the response html inside the out.html file.

You can also pass the url to the script as commandline argument. Check here.


Unfortunately not possible with curl. But you can do this with wget.

$ wget --content-on-error -qO- http://httpbin.org/status/418    -=[ teapot ]=-       _...._     .'  _ _ `.    | ."` ^ `". _,    \_;`"---"`|//      |       ;/      \_     _/        `"""`$ echo $?8


I found a solution because wget was not suitable for sending multipart/form-data

curl -o - -w "\n%{http_code}\n" http://httpbin.org/status/418 | tee >(tail -n 1 | cmp <(echo 2xx) - ) | tee >(grep "char 2"; echo $? > status-code) && grep 0 status-code

Explanation

-o - -w "\n%{http_code}\n" - prints out to stdout (actually it's piped to the next command) with status code at the end
tee - output will be piped to next command and additionally printed to stdout
tail -n 1 - extract status code from the last line
cmp <(echo 2xx) - compare status code, first char only
grep "char 2" - if first character needs to be 2, otherwise fail

In a shell script you can also do better comparison (currently it only allows 2xx, so redirect like 300 are are handled as an error with cmp how it is used above)