【问题标题】:How can I specify content-type accepted when requesting a HTTP resource with Ruby?使用 Ruby 请求 HTTP 资源时,如何指定接受的内容类型?
【发布时间】:2012-02-22 21:52:10
【问题描述】:

我一直使用 open-uri 和 open("").read 通过 http 获取内容。我正在使用它来访问 API,现在我需要指定接受什么内容类型。他们从 curl 中提供了这个例子

curl -LH "Accept: text/bibliography; style=bibtex" http://dx.doi.org/10.1038/nrd842

如何使用 open-uri 或其他 Ruby 函数执行此操作?

【问题讨论】:

    标签: ruby http content-type open-uri


    【解决方案1】:

    我建议使用 open 的可选选项哈希,例如

    open("http://dx.doi.org/10.1038/nrd842","Accept" => "text/bibliography; style=bibtex"){|f| f.each {|line| print line}}
    

    产生结果。

    另见http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.html

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 2015-07-14
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      相关资源
      最近更新 更多