pythonrubyopenwebanalytics

Ruby net/http GET requests with empty body


I'm currently simply trying to get a simple GET request working in Ruby, however, I'm seeing some strange behavior.

I have an Open Web Analytics application running with Docker and it is reachable at http://127.0.0.1:8080/.

I can reach the login site and everything works fine.

Now I want to do a GET request with Ruby to analyze the body of that request but I cannot get it to work, in other languages like Python or simple GET requests over the terminal it works fine. Why not with Ruby?

Here is my very basic Ruby code:

require 'net/http'

url = 'http://127.0.0.1:8080/'
uri = URI(url)

session = Net::HTTP.new(uri.host, uri.port)
response = session.get(uri.request_uri)

puts response.body

Which doesn't output anything. If I look into the NGINX logs from the container, I can see the request being made but there is no further redirection as with the other methods (see below).

172.23.0.1 - - [02/Feb/2023:20:02:59 +0000] "GET / HTTP/1.1" 302 5 "-" "Ruby" "-" 0.088 0.088 . -

If I do a simple GET over the terminal, it works:

GET http://127.0.0.1:8080/

will output the correct body, and in the NGINX logs I can see the following:

172.23.0.1 - - [02/Feb/2023:20:20:10 +0000] "GET / HTTP/1.1" 302 5 "-" "lwp-request/6.61 libwww-perl/6.61" "-" 0.086 0.088 . -
172.23.0.1 - - [02/Feb/2023:20:20:10 +0000] "GET /index.php?owa_do=base.loginForm&owa_go=http%3A%2F%2F127.0.0.1%3A8080%2F& HTTP/1.1" 200 3200 "-" "lwp-request/6.61 libwww-perl/6.61" "-" 0.086 0.088 . -

Doing it in Python with the following basic code also works and gives similar results as with the terminal GET version:

import requests
x = requests.get("http://127.0.0.1:8080/")
print(x.content)

What am I doing wrong?


Solution

  • Got it working with following redirects (see here):

    begin
      response = Net::HTTP.get_response(URI.parse(url))
      url = response['location']
    end while response.is_a?(Net::HTTPRedirection)