End of File error when trying to get response from a url and parsing the JSON with Ruby
I keep getting this error:
Errno::ECONNRESET in SearchController#create
Connection reset by peer
and
EOFError in SearchController#create
end of file reached
I'm trying to get the response from Google's API and then parse the JSON with ruby. Here's my code:
require 'rubygems'
require 'json'
require 'net/https'
def create
@search = params[:search][:search]
base_url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0"
url = "#{base_url}&q=#{@search}&rsz=8&start=0"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
result = JSON.parse(data)
if result.has_key? 'Error'
raise "web service error"
end
return result
end
and resp = Net::HTTP.get_response(URI.parse(url))
seems to be the line giving me the error. How can I fix this?
Here's the first part of the full trace:
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:135:in `sysread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill'
/System/Library开发者_运维技巧/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:62:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/timeout.rb:93:in `timeout'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:116:in `readuntil'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/protocol.rb:126:in `readline'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2020:in `read_status_line'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:2009:in `read_new'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1050:in `request'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:948:in `request_get'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:380:in `get_response'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:543:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:379:in `get_response'
app/controllers/search_controller.rb:10:in `create'
Personally, I recommend using Open::URI
unless you need the lower-level routine's granularity or control:
require 'open-uri'
require 'json'
def create
search = params[:search][:search]
base_url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0"
stream = open("#{base_url}&q=#{search}&rsz=8&start=0")
raise 'web service error' if (stream.status.first != '200')
JSON.parse(stream.read)
end
Open::URI automatically handles redirects and, better yet for this purpose, handles setting up the HTTPs connection for you.
If you want to use Net::HTTP this will work:
require 'rubygems'
require 'json'
require 'net/http'
def create(search)
base_url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0"
url = "#{base_url}&q=#{search}&rsz=8&start=0"
uri = URI.parse(url)
connection = Net::HTTP.new(uri.host, 443)
connection.use_ssl = true
resp = connection.request_get(uri.path + '?' + uri.query)
if resp.code != '200'
raise "web service error"
end
JSON.parse(resp.body)
end
puts create('ruby')
The difference is that I'm telling Net::HTTP to open a SSL connection using port 443, which is where I think your code is failing. Also, I'm looking for the success status code '200', which you might want to check, and then react if you got a redirect.
It might be a problem with Net::HTTP itself. Try using httparty: http://github.com/jnunemaker/httparty
精彩评论