Return code for curl used in a command substitution

53,409

Capturing exit status of commands

The assignment of command output to the rep variable does not lose the exit status of the curl command; it is still available as $?. For more details, see How can I store the return value and/or output of a command in a variable?.

Curl exit code for failed HTTP requests

Usually if a requested HTTP resource isn’t available, a web server responds with a HTML document corresponding to the relevant HTTP status code, e.g. 404. In such cases, curl accepts this document from the server and since it was successful in getting a response from the web server, it displays the error document and exits with a status of 0 for success.

I presume you’re expecting curl to return an error code if there was a problem retrieving the HTTP resource. In scripts it’s better to have curl exit with unsuccessful status if there was a problem retrieving the HTTP resource. For that, you need to provide the -f, --fail option to curl, described in the curl man page as:

Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22.

This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407).

You can save the curl command’s exit status by using an extra variable, status:

httpUrl="http://www.nnin.com"
rep=$(curl -f -v -d "UID=username&PWD=pass" "$httpUrl")
status="$?"
echo "$rep"
exit "$status"

The shell variable, httpUrl should be double-quoted in case the URL contains characters that may be interpreted by the shell. Also, the -X POST is not needed as the POST method is always used when data is provided with the -d option.

(Not) printing output of curl command

There’s no need to save the output of the curl command into the rep variable if all you do with it is print it to standard output.

Regarding the second question, “how to prevent the output from print”, you can replace the -v/--verbose option with the -s/--silent option (Silent mode. Don’t show progress meter or error messages).

In fact, you can simplify your script even more if its last command is the curl invocation. The exit code of the script will be the return code of the script’s last command.

httpUrl="http://www.nnin.com"
curl -f -s -d "UID=username&PWD=pass" "$httpUrl"
Share:
53,409

Related videos on Youtube

Asif
Author by

Asif

Updated on September 18, 2022

Comments

  • Asif
    Asif almost 2 years

    I've script as below :- (edited)

    httpUrl="http://www.nnin.com"
    rep=$(curl -v -X POST -d "UID=username&PWD=pass" $httpUrl)
    
    status=$? 
    
    if [ "$?" -eq 0 ]; then
    
        echo "Success"
        exit $status
      else
        echo "Failed"
        exit $status
    fi
    

    when I run the script the return code always $?=0 because in a way the script is successfully execute but I want the return code for cURL to apply in next function.

    I've try browsing around and use several method in this site particularly but doesn't help much, maybe because of the POST command and how to prevent the output from print when I run the script?

    Thanks

    • Admin
      Admin about 9 years
      Why you set variable rep than echo it instead to output curl directly?
    • Admin
      Admin about 9 years
      You can add code=$? after rep=$(curl... and the last line exit $code
    • Admin
      Admin about 9 years
      can I use $code in another command, does it store curl exit code?
  • not2qubit
    not2qubit over 5 years
    For some reason the example you give does return an error code (3), but anything else doesn't. I.e. it only return 0 with the correct return reason in a json string. Very strange. Perhaps I'm missing something?
  • ingomueller.net
    ingomueller.net over 5 years
    Am I missing something or is the -f option missing in the example script?
  • Anthony Geoghegan
    Anthony Geoghegan over 5 years
    @ingomueller.net Well spotted. :) I've added the -f option into the command. Thanks for the feedback.