2015-09-17 37 views
4

我尝试通过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' 
+0

删除行'puts响应。read_body' – Aguardientico

+0

感谢@Aguardientico跟进。我尝试了您的建议,如更新2所示,但仍然没有运气 – rh4games

+0

您是否尝试在同一服务器实例中同时运行API和客户端?你使用什么服务器? – Aguardientico

回答

0

http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPResponse.html#method-i-read_body

read_body返回身体流,所以你应该重复它,像:

response.read_body do |segment| 
    puts segment 
end 

如果你想得到完整的身体只需使用:response.body

+0

感谢您的快速响应。我用response.body替换了response.read_body,但这并没有改变我的问题。 – rh4games

+0

我唯一能想到的是错误的是在你的第一行代码中缺少'''url = URI('http://192.168.1.5/rest/op/BD1FD3D893613E79)'应该是'url = URI(' http://192.168.1.5/rest/op/BD1FD3D893613E79')' – Aguardientico

+0

谢谢@Aguardientico。我修正了,但仍然没有运气,并且你提出的代码“response.read_body do | segment | puts segment end”产生此错误“IOError(Net :: HTTPOK#read_body called called):” – rh4games

0

看起来非常相似....

拉一组从竹服务器文物(zip文件),并写入到本地存储,当我们越来越截断响应。它只发生在8台服务器中的一台上,并不总是相同的文件。似乎总是缺少文件的最后部分。

使用Ruby 2.0.0p598和net/http。

zip文件的大小范围从2 MB到34 MB。

尽管我们还没有弄清楚为什么响应主体已被截断,我们的解决方法是比较预期的内容长度(在响应头中)和响应主体大小。如果它们不匹配,请再试一次。示例代码:

package = "packages/applicationname.zip" 

def retrieve(package) 

    # generates the Bamboo url for the given package 
    sourceURL = package_url package 

    expectedLength = 0 
    bodyLength = 1 

    # Try to catch the bad download and re-issue the GET request, 
    while expectedLength != bodyLength do 

    # add a counter if worried about getting stuck 

    # issue the request to get the current package zip file 
    response = my_request_wrapper sourceURL 

    expectedLength = response['content-length'].to_i 
    theBody = response.body 
    bodyLength = theBody.size 

    if expectedLength != bodyLength then 
     puts "!! SIZE MISMATCH !!" 
    else 
     # the response body is good, process as needed 
     open package, 'wb' do |io| 
      io.write theBody 
     end 
    end 
    end 
end 

def my_request_wrapper(url) 
    uri = URI.parse(url) 
    http = Net::HTTP.new(uri.host, uri.port) 
    http.use_ssl = true 
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
    req = Net::HTTP::Get.new(uri.request_uri) 
    req.basic_auth("user", "pwd") 
    return http.request req 
end 
0

我使用OSX塞拉利昂,RVM和Ruby 2.3.0

更新我的RVM,红宝石和宝石似乎已经解决了这个问题对我来说

0

我想你在致电http.request之前,必须致电http.start。 不知道为什么,但我看到相同。