Skip to content

Instantly share code, notes, and snippets.

@afalahi
Forked from zaru/request.rb
Created April 5, 2022 19:50
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 afalahi/7908784e3284e1e6762c8c8fdfc53814 to your computer and use it in GitHub Desktop.
Save afalahi/7908784e3284e1e6762c8c8fdfc53814 to your computer and use it in GitHub Desktop.
AWS SNS publish api curl request
require 'json'
require 'aws-sigv4'
require 'active_support/core_ext'
params = {
'Action' => 'Publish',
'TargetArn' => 'arn:aws:sns:ap-northeast-1:000000000000:example-topic',
'Message' => 'message',
'Version' => '2010-03-31'
}.to_query
signer = Aws::Sigv4::Signer.new(
service: 'sns',
region: 'ap-northeast-1',
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
session_token: ENV['AWS_SESSION_TOKEN'] # use STS
)
signature = signer.sign_request(
http_method: 'GET',
url: 'https://sns.ap-northeast-1.amazonaws.com/?' + params
)
cmd = <<EOS
curl -X GET \
-H 'host: #{signature.headers['host']}' \
-H 'x-amz-content-sha256: #{signature.headers['x-amz-content-sha256']}' \
-H 'x-amz-date: #{signature.headers['x-amz-date']}' \
-H 'x-amz-security-token: #{signature.headers['x-amz-security-token']}' \
-H 'authorization: #{signature.headers['authorization']}' \
'https://sns.ap-northeast-1.amazonaws.com/?#{params}'
EOS
puts cmd
puts system(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment