Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTTP/2 POST request in Ruby

I am trying to use the new Apple Push Notification API, which is based on HTTP/2.

I found the http-2 Ruby gem, but the documentation isn’t clear about how to make requests as a client.

How to make a HTTP/2 request in Ruby/Rails?

like image 858
amit_saxena Avatar asked Dec 24 '15 21:12

amit_saxena


1 Answers

DISCLAIMER: I'm the author of the two gems listed here below.

If you want to issue HTTP/2 calls, you may consider NetHttp2, a HTTP/2 client for Ruby.

Usage example for sync calls:

require 'net-http2'

# create a client
client = NetHttp2::Client.new("http://106.186.112.116")

# send request
response = client.call(:get, '/')

# read the response
response.ok?      # => true
response.status   # => '200'
response.headers  # => {":status"=>"200"}
response.body     # => "A body"

# close the connection
client.close    

On top of writing the HTTP/2 calls yourself, if you want an Apple Push Notification gem that uses the new HTTP/2 specifics and can be embedded in a Rails environment then you may also consider Apnotic.

Usage is very simple:

require 'apnotic'

# create a persistent connection
connection = Apnotic::Connection.new(cert_path: "apns_certificate.pem", cert_pass: "pass")

# create a notification for a specific device token 
token = "6c267f26b173cd9595ae2f6702b1ab560371a60e7c8a9e27419bd0fa4a42e58f"

notification       = Apnotic::Notification.new(token)
notification.alert = "Notification from Apnotic!"

# send (this is a blocking call)
response = connection.push(notification)

# read the response
response.ok?      # => true
response.status   # => '200'
response.headers  # => {":status"=>"200", "apns-id"=>"6f2cd350-bfad-4af0-a8bc-0d501e9e1799"}
response.body     # => ""

# close the connection
connection.close
like image 137
ostinelli Avatar answered Sep 28 '22 12:09

ostinelli