Skip to content

Instantly share code, notes, and snippets.

View duncanhall's full-sized avatar
💭
Computers

Duncan Hall duncanhall

💭
Computers
View GitHub Profile
@duncanhall
duncanhall / safe_load_uri.sh
Created January 3, 2018 13:02
Bash / cURL: Load URL contents or fail with HTTP Status code
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"