【问题标题】:net/http rails issue works only in consolenet/http rails 问题仅在控制台中有效
【发布时间】:2016-12-13 01:55:28
【问题描述】:

在 ruby​​ on rails 控制台 'net/http' 中有效,但在控制器中无效并给出超时错误。

require 'net/http'
uri = URI('http://localhost:3000/api_json.json')
json = Net::HTTP.get(uri)
parsed_json = ActiveSupport::JSON.decode(json)

【问题讨论】:

    标签: ruby-on-rails json net-http


    【解决方案1】:

    您很可能使用的是默认 Webrick 服务器,它一次处理一个请求。因此,从控制台它可以正常工作,但是当您尝试从控制器调用它时会失败(当 Webrick 工作人员已经很忙时)。

    您可以尝试设置和运行另一台服务器,如 unicorn 或 Thin,或在不同端口上运行两个 Webrick 实例:

    rails server
    rails server -p 3001
    

    然后转到 localhost:3001

    【讨论】:

      【解决方案2】:

      @dimuch 的解决方案可能已经解决了您的问题,但它可能会帮助面临类似情况的人。我将详细解释问题和解决方案(@dimuch 解决方案的扩展)。

      问题:

      您可能有一个类似于 some:"/test_controller/test_method" 的控制器,并且您可能希望调用控制器中的一个方法,例如 /api/v1/some_test_api,并面临诸如 Completed 500 Internal Server Error in 60004.4ms 之类的错误 [27580c5c46770812c550188346c2dd3e] [127.0.0.1] [/xauth_test/sanity_oauth_login] 超时::错误(超时::错误):

      解决方案:

      正如@dimuch 所说,“ 很可能您使用的是默认 Webrick 服务器,它一次服务一个请求....."。1. 您需要在不同的端口上运行应用程序,例如 rails s -p 3000 和 rails s -p 3001,然后从 3001 发出请求。 如果您遇到诸如“服务器已在运行。检查 /tmp/pids/server.pid。退出”之类的问题,请尝试运行 rails s -p 3001 -P PROCESS_ID。 2.使用其他服务器,如独角兽或彪马。

      注意:如果您只想在本地进行测试,那么我建议您使用第一个解决方案,它简单易行。我很抱歉英语不好,我从其他堆栈溢出页面和网站中找到了大多数解决方案,我在下面附上了(参考链接),如果我错过了一些要参考的东西,我很抱歉。希望这对某人有所帮助。

      参考: 运行多个实例:

      Running multiple instances of Rails Server

      类似的错误及其处理方式:

      Rails HTTParty Getting Timeout::Error

      Faraday timeout error with omniauth (custom strategy)/doorkeeper

      Strange Timeout::Error with render_to_string and HTTParty in Controller Action

      配置独角兽和彪马:

      http://vladigleba.com/blog/2014/03/21/deploying-rails-apps-part-3-configuring-unicorn/

      https://github.com/puma/puma

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 2015-08-19
        相关资源
        最近更新 更多