如何在OpenURI中指定"http请求标头"

iwa*_*wan 33 ruby open-uri

我试图使用Ruby的OpenURI gem调用URL,但是它需要我在其HTTP请求头中传递某些值.

知道怎么做吗?

cam*_*cam 49

根据文档,您可以将http头的哈希值作为第二个参数传递给open:

open("http://www.ruby-lang.org/en/",
   "User-Agent" => "Ruby/#{RUBY_VERSION}",
   "From" => "foo@bar.invalid",
   "Referer" => "http://www.ruby-lang.org/") {|f|
   # ...
 }
Run Code Online (Sandbox Code Playgroud)