Ruby http Get返回截断响应

Ruby http Get返回截断响应,http,http-get,postman,Http,Http Get,Postman,我尝试通过Postman向设备的Restful API发送HTTP Get,结果很好,返回了我期望的所有文本。Postman建议请求的Ruby代码如下: 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' reque

我尝试通过Postman向设备的Restful API发送HTTP Get,结果很好,返回了我期望的所有文本。Postman建议请求的Ruby代码如下:

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
但是,当我在代码中尝试这样做时,它返回一个被截断的响应(缺少行),我必须多次重新发送相同的Get以获得整个文本响应

上面的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
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):
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'
更新2

我试过这个

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
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):
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'
基于:

read_body
将主体作为流返回,因此您应该迭代它,类似于:

response.read_body do |segment|
  puts segment
end
如果你想得到完整的身体,只需使用:
response.body

看起来非常相似

当我们从Bamboo服务器中提取一组工件(zip文件)并写入本地存储时,会得到截断的响应。它只在8台服务器中的一台服务器上发生,而且并不总是在同一个文件上发生。似乎总是缺少文件的最后一部分

使用Ruby 2.0.0p598和net/http

zip文件的大小从2MB到34MB不等

虽然我们还没有弄清楚响应主体被截断的原因,但我们的解决方法是将预期的内容长度(在响应头中)与响应主体大小进行比较。如果它们不匹配,请重试。示例代码:

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

我使用的是OSX Sierra、rvm和ruby 2.3.0


更新我的rvm、ruby和gems似乎已经解决了我的问题

我认为您必须在调用
http.request
之前调用
http.start

不知道为什么,但我也看到了同样的情况。

谢谢您的快速回复。我将response.read_body替换为response.body,但这并没有改变我的问题。我认为唯一错误的是您的第一行代码
url=URI('http://192.168.1.5/rest/op/BD1FD3D893613E79)
应该是
url=URI('http://192.168.1.5/rest/op/BD1FD3D893613E79')
谢谢@aguardiencico。我修复了这个问题,但仍然没有成功,您提出的代码“response.read_body do | segment | put segment end”生成了这个错误“IOError(Net::HTTPOK#read_body调用了两次):“是的,它生成了这个错误”IOError(Net::HTTPOK#read_body调用了两次):“你能用产生错误的代码更新问题吗?删除行
put response。阅读正文
感谢@aguardiencio的跟进。我尝试了您的建议,如更新2中所示,但仍然没有luckAre您尝试在同一服务器实例中同时运行API和客户端吗?你在用什么服务器?@Aguardiencio:谢谢你的跟进。客户端是我的RoR应用程序,服务器是第三方设备。我不知道那里运行的是什么服务器。我只是有使用他们API的说明,当我使用Postman时,它工作正常,但我的应用程序如上所述出现故障。@rh4games-你解决了这个问题吗?