Using NetHTTP within Rails to hit Rails

≡放荡痞女 提交于 2019-12-07 14:53:04

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/16970242/using-nethttp-within-rails-to-hit-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!