4

We have a very unique use case where we want a Rails controller to access a route within the Rails app using Net::HTTP. Can this be done? I'm currently receiving a timeout when attempting to do so. The current code works when the uri is a separate Rails app, but not when the uri belongs to the app itself. Here's the gist of the current controller action:

def export_data
  uri = URI("http://localhost:3000")
  @data = JSON.parse( Net::HTTP.get(uri) )

  respond_to do |format|
    ...
  end
end

Forget why we want to do this. Why doesn't this work? Is there a modification that can be made to get it to work? Thanks in advance!

Rebitzele
  • 3,252
  • 1
  • 20
  • 21

1 Answers1

8

It doesn't work because you are not using a multi-threaded server. Your request is coming in and blocking the server until it's complete. During that time, you're making a request to your localhost that isn't being handled.

Easy solution? Try puma. Other easy solution, spin up two rails instances, connect to the 2nd instance.

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
  • 1
    Ah! Thanks so much! So, to clarify: Since Thin (the localhost server we're using) is in the middle of sending a request, it can't also receive a request. Is that right? – Rebitzele Jun 06 '13 at 19:18
  • That's correct. If you spin up a localhost:3001, you could connect to it. (or have a multithreaded server) – Jesse Wolgamott Jun 06 '13 at 19:19