Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top