【问题标题】:How to specify "http request header" in OpenURI如何在 OpenURI 中指定“http 请求标头”
【发布时间】:2011-11-20 16:28:17
【问题描述】:

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

知道怎么做吗?

【问题讨论】:

  • 这确实有助于解决 422 错误,这是由于 Accept 参数期望知道像 xml 这样的格式

标签: ruby open-uri


【解决方案1】:

根据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|
   # ...
 }

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2023-03-21
    • 2017-10-07
    • 2015-07-13
    • 1970-01-01
    相关资源
    最近更新 更多