我一直使用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 的 optional 散列,例如
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
关于ruby - 使用 Ruby 请求 HTTP 资源时如何指定接受的内容类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9403661/