Skip to content

Instantly share code, notes, and snippets.

@abbaspour
Created April 17, 2022 21:12
Show Gist options
  • Save abbaspour/9680673dca3ce40c473d38510861cc7f to your computer and use it in GitHub Desktop.
Save abbaspour/9680673dca3ce40c473d38510861cc7f to your computer and use it in GitHub Desktop.
Compressed Multipart Form Data POST with CURL
#!/usr/bin/env bash
set -eufo pipefail
declare -A form_parts
function usage() {
cat <<END >&2
USAGE: $0 [-F key=value] [-F key=@file] [curl-opts] [-v|-h]
-F key=value # curl post key=value
-h|? # usage
-v # verbose
eg,
$0 -F connection_id=${users_db_id} -F upsert=false -F send_completion_email=false -F users=@users.json
END
exit $1
}
function build_multipart() {
local -n b=$1
local -n parts=$2
for key in "${!parts[@]}"; do
printf "%s\r\n" "--${b}"
local value="${parts[$key]}"
if [[ $value == @* ]]; then
local file="${value#?}"
[[ ! -f "${file}" ]] && { echo >&2 "ERROR: file not found: ${file}"; exit 3; }
printf "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n" "${key}" "$(basename "${file}")"
#printf "Content-Type: %s\r\n\r\n" "$(file --mime-type -b "${file}")"
printf "Content-Type: application/octet-stream\r\n\r\n"
cat "${file}"
else
printf "Content-Disposition: form-data; name=\"%s\"\r\n\r\n" "${key}"
echo -n "${value}"
fi
printf "\r\n"
done
printf "%s\r\n" "--${b}--"
}
function get_boundary() {
echo $RANDOM | md5sum | head -c 32
}
function content_type_header() {
echo "Content-Type: multipart/form-data; boundary=$1"
}
while getopts "F:hv?" opt; do
case ${opt} in
F) form_parts[${OPTARG%%=*}]=${OPTARG#*=} ;;
?) usage 0 ;;
esac
done
readonly boundary=$(get_boundary)
export AUTH0_DOMAIN_URL=$(echo "${access_token}" | awk -F. '{print $2}' | base64 -di 2>/dev/null | jq -r '.iss')
# --trace-ascii trace-multipart.txt
build_multipart boundary form_parts | gzip | curl \
-H "$(content_type_header "${boundary}")" \
-H "Authorization: Bearer ${access_token}" \
-H "Content-Encoding: gzip" \
--url "${AUTH0_DOMAIN_URL}api/v2/jobs/users-imports" \
--data-binary @-
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment