Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active August 9, 2018 13:58
Show Gist options
  • Save magnetikonline/120ac343a5e46e14ca5d to your computer and use it in GitHub Desktop.
Save magnetikonline/120ac343a5e46e14ca5d to your computer and use it in GitHub Desktop.
Simulate CORS GET web requests using curl.
#!/bin/bash -e
LINE_BREAK="======================================"
REQUEST_HEADERS="Content-Type"
REQUEST_METHOD="GET"
function exitError {
echo "Error: $1" >&2
exit 1
}
function usage {
cat <<EOM
Usage: $(basename "$0") [OPTION]...
-r URL request URL
-o DOMAIN origin domain
-h display help
EOM
exit 2
}
# read arguments
requestURL=""
originDomain=""
while getopts ":r:o:h" optKey; do
case $optKey in
r)
requestURL=$OPTARG
;;
o)
originDomain=$OPTARG
;;
h|*)
usage
;;
esac
done
# validation of arguments
[[ -z $requestURL ]] && exitError "Missing request URL"
[[ -z $originDomain ]] && exitError "Missing origin domain"
# make OPTIONS call
curl \
--dump-header - \
--header "Access-Control-Request-Headers: $REQUEST_HEADERS" \
--header "Access-Control-Request-Method: $REQUEST_METHOD" \
--header "Origin: $originDomain" \
--output /dev/null \
--request OPTIONS \
--silent \
"$requestURL"
echo "======================================"
echo
# make GET call
curl \
--dump-header - \
--header "Origin: $originDomain" \
--request $REQUEST_METHOD \
--silent \
"$requestURL"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment