EOFError (end of file reached) in Ruby on Rails with http.request

I am trying to get json form url:

uri = URI.parse("http://84.38.185.251:9262/send") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) response.code # => 301 response.body # => The body (HTML, XML, blob, whatever) response["cache-control"] # => public, max-age=2592000 puts response.body 

but I get an error: `EOFError (end of file reached): app / controllers / sensors_controller.rb: 35: in sensinfo '

sensors_controller.rb: 35:

 response = http.request(request) 

What have I done wrong?

+6
source share
3 answers

I think this is some kind of mistake; typhoeus works:

 require 'typhoeus' response = Typhoeus.get("http://84.38.185.251:9262/send") p response.body #=> {"ids":"-1","data":{"temp":"nan","h":"-1"},"status":"255","voltage":"-1"} 
0
source

this error is most often used to use https

If it is https , then

Please try this

 uri = URI.parse("https://84.38.185.251:9262/send") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) http.use_ssl = true response = http.request(request) 

Adicalal note

 http.use_ssl = true 

If it is not https

 http.use_ssl = false 

or you can add a condition

 http.use_ssl = true if domain =~ /^https/ 

you can get more at this http://expressica.com/2012/02/10/eoferror-end-of-file-reached-issue-when-post-a-form-with-nethttp/

+12
source

I ran into this recently and eventually found that it was caused by a network timeout from the endpoint we hit. Luckily for us, we were able to increase the timeout duration.

To make sure this is our problem (and not really a problem with net http), I made the same request using curl and confirmed that the request was aborted.

0
source

All Articles