如何在使用Ruby请求HTTP资源时指定接受的内容类型?

时间:2012-02-22 21:52:10

标签: ruby http content-type open-uri

我一直使用open-uri,并打开(“”)。阅读以通过http获取内容。我使用它来访问API,现在我需要指定接受哪种内容类型。他们从curl

提供了这个例子
curl -LH "Accept: text/bibliography; style=bibtex" http://dx.doi.org/10.1038/nrd842

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

1 个答案:

答案 0 :(得分:2)

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

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