Why am I getting "Errno :: ECONNREFUSED" with "net / http" in Rails?

I am trying to parse an XML file from a url. When I try something like this:

require 'net/http' require 'rubygems' require 'xmlsimple' url = 'http://my-address.com/xmltest/note.xml' xml_data = Net::HTTP.get_response(URI.parse(url)).body 

Everything works, but only when I do it outside of my Rails project. If I try to include this file in a Rails 3 project and parse it there, I get the error message "Errno::ECONNREFUSED in [controller]" - Connection refused - connect(2) .

My problem is this: I do not know how to install the net/http component. I am looking for him at http://www.rubygems.org , but I cannot find him.

+6
source share
2 answers

Net::HTTP is part of the Ruby standard library. You can use require 'net/http' to download it.

Instead of using Net::HTTP , which is pretty low for what you want to do, I would recommend using Ruby Open::URI .

If you are moving a lot of HTTP data, you may need to look at something like HTTPClient or Curb or Typhoeus, which are designed to be used under difficult conditions and save you from having to write everything using Net::HTTP .

Regarding the ECONNREFUSED error: you might want to grab the HTTPResponse returned from get_response and then check its status before trying to read the body. The way you do it now does not allow you to respond to a failed request.

+6
source

It seems that the problem is that you cannot connect to the HTTP server.
You can try to verify that you can access the URL in the browser.

+2
source

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


All Articles