I am trying to do a CURL with an IF Else condition. On success of the call Print a successful message or else Print the call failed.
My Sample Curl would look like:
curl ' > HTML_Output.html I want to do the same thing using Shell.
Using JavaScript:
if(res.status === 200){console.log("Yes!! The request was successful")} else {console.log("CURL Failed")} Also, I see the CURL percentage, but I do not know, how to check the percentage of CURL. Please help.
13 Answers
You can use the -w (--write-out) option of curl to print the HTTP code:
curl -s -w '%{http_code}\n' ' It will show the HTTP code the site returns.
Also curl provides a whole bunch of exit codes for various scenarios, check man curl.
One way of achieving this like,
HTTPS_URL="" CURL_CMD="curl -w httpcode=%{http_code}" # -m, --max-time <seconds> FOR curl operation CURL_MAX_CONNECTION_TIMEOUT="-m 100" # perform curl operation CURL_RETURN_CODE=0 CURL_OUTPUT=`${CURL_CMD} ${CURL_MAX_CONNECTION_TIMEOUT} ${HTTPS_URL} 2> /dev/null` || CURL_RETURN_CODE=$? if [ ${CURL_RETURN_CODE} -ne 0 ]; then echo "Curl connection failed with return code - ${CURL_RETURN_CODE}" else echo "Curl connection success" # Check http code for curl operation/response in CURL_OUTPUT httpCode=$(echo "${CURL_OUTPUT}" | sed -e 's/.*\httpcode=//') if [ ${httpCode} -ne 200 ]; then echo "Curl operation/command failed due to server return code - ${httpCode}" fi fi 1Like most programs, curl returns a non-zero exit status if it gets an error, so you can test it with if.
if curl ' > HTML_Output then echo "Request was successful" else echo "CURL Failed" fi I don't know of a way to find out the percentage if the download fails in the middle.
2