Skip to content

Instantly share code, notes, and snippets.

@DiegoSalazar
Created October 29, 2014 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiegoSalazar/14689d78925e0ef101d4 to your computer and use it in GitHub Desktop.
Save DiegoSalazar/14689d78925e0ef101d4 to your computer and use it in GitHub Desktop.
API request signature and test
#!/bin/bash
SECRET_KEY=$TEST_SENDER_KEY
ACCESS_ID=$TEST_SENDER_TOKEN
API_BASE=$TEST_ENDPOINT_BASE
query="{\"document\":{\"recipient_id\":\"$TEST_RECIPIENT_TOKEN\",\"data\":{\"id\":\"1\",\"first_name\":\"Bill\"}}}"
content_md5=$(echo -n $query | openssl md5 -binary | base64)
content_type='application/json'
request_uri='/api'
httpdate=$(date -u +"%a, %_d %b %Y %H:%M:%S GMT")
accept_header='application/vnd.test+json; version=1'
canonical_string="$content_type,$content_md5,$request_uri,$httpdate"
signature=$(echo -n $canonical_string | openssl sha1 -hmac $SECRET_KEY -binary | base64)
echo "Sending request: $API_BASE$request_uri"
echo "Query: $(echo $query | python -m json.tool)"; echo
curl -H "Authorization: APIAuth $ACCESS_ID:$signature" \
-H "Content-MD5: $content_md5" \
-H "Date: $httpdate" \
-H "Accept: $accept_header" \
-H "Content-type: $content_type" \
-d "$query" \
-v \
$API_BASE$request_uri
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment