【发布时间】:2011-11-20 16:28:17
【问题描述】:
我正在尝试使用 Ruby 的 OpenURI gem 调用 URL,但是它需要我在其 HTTP 请求标头中传递某些值。
知道怎么做吗?
【问题讨论】:
-
这确实有助于解决 422 错误,这是由于 Accept 参数期望知道像 xml 这样的格式
我正在尝试使用 Ruby 的 OpenURI gem 调用 URL,但是它需要我在其 HTTP 请求标头中传递某些值。
知道怎么做吗?
【问题讨论】:
根据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|
# ...
}
【讨论】: