Created
January 3, 2018 13:02
-
-
Save duncanhall/53752262119d56f1d90ee26b4620753d to your computer and use it in GitHub Desktop.
Bash / cURL: Load URL contents or fail with HTTP Status code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
safe_load_uri() { | |
local URL="$1" | |
local RESPONSE="$(curl --silent --write-out "HTTPSTATUS:%{http_code}" -X GET $URL)" | |
local BODY="$(echo $RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')" | |
local STATUS="$(echo $RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')" | |
if [ $STATUS -ge 400 ]; then | |
echo "$STATUS" | |
exit 1 | |
else | |
echo "$BODY" | |
fi | |
} | |
if RESULT=$(safe_load_uri "https://duncanhall.net"); then | |
echo "$RESULT" | |
else | |
echo "Failed: $RESULT" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment