ruby-on-rails - 在 Ruby 中发送 HTTP/2 POST 请求

标签 ruby-on-rails ruby http http2

我正在尝试使用新的 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/

相关文章:

java - 使用 Java restful HTTP 连接到 OrientDB 服务器

css - 如何 float 或显示内嵌表格

ruby-on-rails - 尝试在 xubuntu 14.04 上安装 rails 错误 : Failed to build gem native extension

python - 在 Python CGI 中返回 http 状态代码

ruby-on-rails - ruby 参数后面的冒号是什么意思?

ruby-on-rails - 仅在调用某些方法时验证 Rails 模型

ios - 在 iOS Swift 中将远程 JSON 数据同步到本地缓存存储

ruby-on-rails - 使用有效的 access_token 时缺少 token 端点 URI

ruby-on-rails - 将 Mac Pro 中的操作系统更新为 mavericks 后无法创建 Rails 应用程序

ruby-on-rails - Rspec:测试嵌套 Controller