如何使用修改后的标头进行 HTTP GET?
在 Ruby 中使用修改后的标头发出 HTTP GET 请求的最佳方法是什么?
我想从日志文件末尾获取一定范围的字节,并且一直在玩弄以下代码,但是服务器返回一个响应,表示“这是服务器无法理解的请求”(服务器是阿帕奇)。
require 'net/http'
require 'uri'
#with @address, @port, @path all defined elsewhere
httpcall = Net::HTTP.new(@address, @port)
headers = {
'Range' => 'bytes=1000-'
}
resp, data = httpcall.get2(@path, headers)
- 有没有更好的方法在 Ruby 中定义标头?
- 有谁知道为什么这对 Apache 会失败? 如果我在浏览器中访问
http://[address]:[port]/[path]
我会毫无问题地获得我正在寻找的数据。
What is the best way to make an HTTP GET request in Ruby with modified headers?
I want to get a range of bytes from the end of a log file and have been toying with the following code, but the server is throwing back a response saying that "it is a request that the server could not understand" (the server is Apache).
require 'net/http'
require 'uri'
#with @address, @port, @path all defined elsewhere
httpcall = Net::HTTP.new(@address, @port)
headers = {
'Range' => 'bytes=1000-'
}
resp, data = httpcall.get2(@path, headers)
- Is there a better way to define headers in Ruby?
- Does anyone know why this would be failing against Apache? If I do a get in a browser to
http://[address]:[port]/[path]
I get the data I am seeking without issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建了一个对我有用的解决方案(效果很好) - 这个示例获取范围偏移:
Created a solution that worked for me (worked very well) - this example getting a range offset:
如果您有权访问服务器日志,请尝试将浏览器的请求与 Ruby 的请求进行比较,看看是否能告诉您任何信息。 如果这不切实际,请启动 Webrick 作为文件服务器的模拟。 不用担心结果,只需比较请求,看看它们做了什么不同的事情。
至于 Ruby 风格,您可以内联移动标头,如下所示:
另外,请注意,在 Ruby 1.8+ 中,您几乎可以肯定正在运行的内容,
Net::HTTP#get2
返回单个HTTPResponse
对象,而不是resp, data
对。If you have access to the server logs, try comparing the request from the browser with the one from Ruby and see if that tells you anything. If this isn't practical, fire up Webrick as a mock of the file server. Don't worry about the results, just compare the requests to see what they are doing differently.
As for Ruby style, you could move the headers inline, like so:
Also, note that in Ruby 1.8+, what you are almost certainly running,
Net::HTTP#get2
returns a singleHTTPResponse
object, not aresp, data
pair.