Skip to content

Instantly share code, notes, and snippets.

@tamphh
Created March 4, 2019 14:58
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 tamphh/13d0716cd9be112869990a3add14f28e to your computer and use it in GitHub Desktop.
Save tamphh/13d0716cd9be112869990a3add14f28e to your computer and use it in GitHub Desktop.
Ruby script to send POST HTTPS request sample
# 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