我正在尝试使用新的 Apple Push Notification API,基于HTTP/2 .
我找到了 http-2 Ruby gem,但文档并不清楚如何作为客户端发出请求。
如何在 Ruby/Rails 中发出 HTTP/2 请求?
最佳答案
免责声明:我是下面列出的两个 gem 的作者。
如果你想发出 HTTP/2 调用,你可以考虑 NetHttp2 ,Ruby 的 HTTP/2 客户端。
同步调用的使用示例:
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
除了自己编写 HTTP/2 调用之外,如果您想要一个使用新的 HTTP/2 特性并且可以嵌入到 Rails 环境中的 Apple 推送通知 gem,那么您还可以考虑 Apnotic .
用法很简单:
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
关于ruby-on-rails - 在 Ruby 中发送 HTTP/2 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34458223/