0

I'm trying to make a get request to a service of mine with a valid URL string (if I put it into my browser, I get the expected response). However, when I run the following function:

def dispatch_uri(url)
  uri = Addressable::URI.parse(url)

  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.request_uri)

  response = http.request(request).to_s
  response 
end

The response variable holds a Net::HTTPVersionNotSupported, which has no body and isn't, of course, the expected response.

What am I doing wrong and how should I address this problem?

RafaelCruz
  • 60
  • 5
  • That looks like a 505 error from the server. It's saying your request is not supported by the server. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/505 – Derek Hopper Dec 21 '17 at 19:10
  • But I can run the URL I used to create the URI and get the expected response. How is it not supported? – RafaelCruz Dec 22 '17 at 13:27

1 Answers1

0

So, the answer is simpler than I thought.

Net::HTTP is both unable to work with an UTF-8 URL or Addressable::URI, however, Addressable gives us a fantastic tool to solve this problem: normalize.

Normalize converts your UTF=8 to a codified ASCII HTML compatible string, so a working code is:

def dispatch_uri(url)
  uri = URI(Addressable::URI.parse(url).normalize.to_s)
  response = Net::HTTP.get(uri)
  response
end

This normalized string can be used to create a standard URI object and, thus, you are able to use a regular Net::HTTP request.

RafaelCruz
  • 60
  • 5