我尝试通过Postman向设备的Restful API发送HTTP Get,并且它很好地返回了我期待的所有文本。邮差建议请求Ruby代码为以下几点:Ruby http获取返回截断的响应
url = URI('http://192.168.1.5/rest/op/BD1FD3D893613E79')
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
request.basic_auth 'admin', 'admin'
request["accept"] = 'Application/json'
response = http.request(request)
puts response.read_body
但是当我试图在我的代码,它返回一个截断的响应(线条缺失)和我有相同的获取多次重发,以获得整个文本响应响应。
上面的Ruby代码中是否缺少导致此截断响应的错误?
更新1
我尝试这样
url = URI('http://192.168.1.5/rest/op/BD1FD3D893613E79')
http = Net::HTTP.new(url.host, url.port)
request = Net::HTTP::Get.new(url)
request.basic_auth 'admin', 'admin'
request["accept"] = 'Application/json'
response = http.request(request)
puts response.read_body
response.read_body do |segment|
puts segment.to_s
end
和产生此错误
IOError (Net::HTTPOK#read_body called twice):
更新2
我尝试这个
1073 url = URI('http://192.168.1.5/rest/op/BD1FD3D893613E79')
1074 http = Net::HTTP.new(url.host, url.port)
1075 request = Net::HTTP::Get.new(url.to_s)
1076 request.basic_auth 'admin', 'admin'
1077 request["accept"] = 'Application/json'
1078 response = http.request(request)
1079 response.read_body do |segment|
1080 puts segment.to_s
1081 end
,并基于得到这个错误
IOError (Net::HTTPOK#read_body called twice):
app/controllers/Apps_controller.rb:1079:in `block in get_config'
app/controllers/Apps_controller.rb:1045:in `each'
app/controllers/Apps_controller.rb:1045:in `get_config'
删除行'puts响应。read_body' – Aguardientico
感谢@Aguardientico跟进。我尝试了您的建议,如更新2所示,但仍然没有运气 – rh4games
您是否尝试在同一服务器实例中同时运行API和客户端?你使用什么服务器? – Aguardientico