Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NetHTTP within Rails to hit Rails

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!

like image 243
Rebitzele Avatar asked Feb 16 '23 07:02

Rebitzele


1 Answers

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.

like image 198
Jesse Wolgamott Avatar answered Feb 27 '23 11:02

Jesse Wolgamott