我一直使用 open-uri 和 open("").read 通过 http 获取内容。我正在使用它来访问 API,现在我需要指定接受什么内容类型。他们从 curl 提供了这个例子
curl -LH "Accept: text/bibliography; style=bibtex" http://dx.doi.org/10.1038/nrd842
如何使用 open-uri 或其他 Ruby 函数执行此操作?
我一直使用 open-uri 和 open("").read 通过 http 获取内容。我正在使用它来访问 API,现在我需要指定接受什么内容类型。他们从 curl 提供了这个例子
curl -LH "Accept: text/bibliography; style=bibtex" http://dx.doi.org/10.1038/nrd842
如何使用 open-uri 或其他 Ruby 函数执行此操作?
我建议使用 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