使用http.request在Ruby on Rails中使用EOFError(文件末尾)

时间:2014-04-15 09:49:06

标签: ruby-on-rails ruby http get

我正在尝试获取json表单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

但是我收到一个错误:`EOFError(到达文件末尾):   app / controllers / sensors_controller.rb:35:在sensinfo'

sensors_controller.rb:35:

response = http.request(request)

我做错了什么?

2 个答案:

答案 0 :(得分:12)

此错误主要用于使用https

如果是https那么

请尝试这个

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)

注意aditional

http.use_ssl = true

如果不是https

http.use_ssl = false

或者您可以添加条件

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

您可以在http://expressica.com/2012/02/10/eoferror-end-of-file-reached-issue-when-post-a-form-with-nethttp/

上获得更多信息

答案 1 :(得分:0)

我认为这是一种某种错误; typhoeus似乎有效:

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