在Ruby中发送HTTP / 2 POST请求 [英] Sending HTTP/2 POST request in Ruby
本文介绍了在Ruby中发送HTTP / 2 POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用新的 Apple推送通知 API,基于 HTTP / 2 。
I am trying to use the new Apple Push Notification API, which is based on HTTP/2.
我找到了 http-2 Ruby gem,但文档并不清楚如何作为客户端发出请求。
I found the http-2 Ruby gem, but the documentation isn’t clear about how to make requests as a client.
如何在Ruby / Rails中发出HTTP / 2请求?
How to make a HTTP/2 request in Ruby/Rails?
推荐答案
有一个在中创建HTTP / 2客户端的示例这个档案。它可以适应这样的APN请求:
There is an example of creating HTTP/2 client in this file. It could be adapted to APN requests like this:
require 'socket'
require 'http/2'
payload = '{"foo":"bar"}'
device_token = '00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0' # example
sock = TCPSocket.new('api.development.push.apple.com', 443)
conn = HTTP2::Client.new
conn.on(:frame) do |bytes|
puts "Sending bytes: #{bytes.unpack("H*").first}"
sock.print bytes
sock.flush
end
stream = conn.new_stream
stream.on(:close) do
sock.close
end
head = {
':scheme' => 'https',
':method' => 'POST',
':path' => "/3/device/#{device_token}",
'apns-id' => '123e4567-e89b-12d3-a456-42665544000', # or you could omit this header
'content-length' => payload.bytesize.to_s # should be less than or equal to 4096 bytes
}
puts 'Sending HTTP 2.0 request'
stream.headers(head, end_stream: false)
stream.data(payload)
while !sock.closed? && !sock.eof?
data = sock.read_nonblock(1024)
puts "Received bytes: #{data.unpack("H*").first}"
begin
conn << data
rescue => e
puts "Exception: #{e}, #{e.message} - closing socket."
sock.close
end
end
这篇关于在Ruby中发送HTTP / 2 POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文