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

时间:2011-09-20 00:28:02

标签: ruby open-uri

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

知道怎么做吗?

1 个答案:

答案 0 :(得分:48)

根据the documentation,您可以将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|
   # ...
 }
相关问题