EgorkZe
EgorkZe

Reputation: 403

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 am i did wrong?

Upvotes: 9

Views: 24572

Answers (2)

Rajarshi Das
Rajarshi Das

Reputation: 12340

this error mostly get for using https

If it is https then

Please try this one

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)

Note aditional

http.use_ssl = true

If it is not https

http.use_ssl = false

or you can add the condition

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

you can get more on this https://web.archive.org/web/20140226183826/http://expressica.com/2012/02/10/eoferror-end-of-file-reached-issue-when-post-a-form-with-nethttp/

Upvotes: 18

mdesantis
mdesantis

Reputation: 8517

I think it is a some sort of bug; typhoeus seems to work:

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"}

Upvotes: 0

Related Questions