Created
March 4, 2019 14:58
-
-
Save tamphh/13d0716cd9be112869990a3add14f28e to your computer and use it in GitHub Desktop.
Ruby script to send POST HTTPS request sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# source: http://www.rubyinside.com/nethttp-cheat-sheet-2940.html | |
# FCM ref: https://medium.com/@cdmunoz/working-easily-with-fcm-push-notifications-in-android-e1804c80f74 | |
# How to run in terminal: ruby test_fcm.rb | |
require 'net/https' | |
require 'uri' | |
require 'json' | |
uri = URI.parse('https://fcm.googleapis.com/fcm/send') | |
device_token = 'FCM_device_token' | |
header = { | |
'Authorization': 'key=Legacy_server_key', | |
'Content-Type': 'application/json' | |
} | |
form_data = { | |
to: device_token, | |
data: { | |
alert: 'You received a notification!', | |
badge: 1, | |
body: 'Some body' | |
} | |
} | |
# Create the HTTP objects | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request = Net::HTTP::Post.new(uri.request_uri, header) | |
request.body = form_data.to_json | |
# Send the request | |
response = http.request(request) | |
puts JSON.pretty_generate(JSON.parse(response.body)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment