Ruby http Get a returned truncated response

I tried sending an HTTP Get to the Restful API device via Postman, and it worked fine, returning all the text I was expecting. Postman suggested Ruby code for the request:

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

but when I tried this in my code, it returns a truncated response (missing lines), and I need to resubmit the same Get several times to get the whole response from the text response.

Is there anything in the Ruby code above that triggers this truncated answer?

Update 1

I tried this

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

and which generated this error

IOError (Net::HTTPOK#read_body called twice):

Update 2

I tried this

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

and got this error

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'
+4
source share
4 answers

: 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

....

(zip ) Bamboo . , 8 , . , .

Ruby 2.0.0p598 net/http.

zip 2 34 .

, , ( ) . , . :

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 Sierra, rvm ruby ​​2.3.0

Updating my rvm, ruby ​​and gems seems to fix the issue for me

0
source

I think you should call http.startbefore you call http.request. I don’t know why, but I see the same thing.

0
source

Source: https://habr.com/ru/post/1607752/


All Articles