Rails HTTParty 获取超时::错误 [英] Rails HTTParty Getting Timeout::Error
问题描述
我有一个以 API 为中心的应用程序 (/api/v1/users),它简单地以 JSON 格式返回所有用户.
I have an API centric application (/api/v1/users) it simply return all users restfully with JSON format.
我的问题是,如果我在控制器上调用该路由,它会返回Timeout::Error"有什么问题?
My problem is, if I call that route on the controller, it returns "Timeout::Error" What is the problem?
class BaseController < ApplicationController
def index
return HTTParty.get('http://localhost:3000/api/v1/users').body
end
end
更新
- users_controller.rb (/api/v1/users)
- application_controller.rb
https://gist.github.com/4359591
日志
推荐答案
如果我理解正确,您有一个 API 端点,位于 /api/v1/users
,以及您的 BaseController#index
正在调用该方法?
If I understand correctly, you have an API end-point, at /api/v1/users
, and your BaseController#index
is calling that method?
如果这是正确的,在同一个 rails 进程中,并且您正在开发模式下进行测试(我可以从您的 url 中看出),那么您只有一个进程在运行,它一次只能处理一个请求.因此,如果您向 BaseController#index
发起请求,它将向您自己的测试服务器发起另一个请求,该服务器正忙,并且会一直等到超时.
If that is correct, inside the same rails process, and you are testing in development mode (as I can tell from your url), then you only have a single process running, which can only handle a single request at once. So if you start a request to BaseController#index
, it will start another request to your own test-server, which is busy, and it will just wait until it times out.
如果您想测试您的 API,我会查看客户端工具,例如邮递员.
If you want to test your API, I would look at a client tool like e.g. Postman.
HTH.
这篇关于Rails HTTParty 获取超时::错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!