gpt4 book ai didi

ruby-on-rails - Net::HTTP::Get 超时问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:49 26 4
gpt4 key购买 nike

尝试从索引 Controller 操作中调整一段简单的代码。
但是得到 Net::ReadTimeout 错误。

class PaymentMethodsController < ApplicationController
def index
uri = URI.parse("http://localhost:3000")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new("/api/user/profile/payment", {'Content-Type' =>'application/json'})
request.set_form_data({token: 'vjuri@mrd.com:CFTKGNZKXC'})
res = http.request(request)

render text: res.body
end
end

在 Rails 控制台中,它的工作原理非常棒。

2.1.2 :027 > uri = URI.parse("http://localhost:3000") => #<URI::HTTP:0x00000004d0eb80 URL:http://localhost:3000> 
2.1.2 :028 > http = Net::HTTP.new(uri.host, uri.port) => #<Net::HTTP localhost:3000 open=false>
2.1.2 :029 > request = Net::HTTP::Get.new("/api/user/profile/payment", {'Content-Type' =>'application/json'})
=> #<Net::HTTP::Get GET>
2.1.2 :031 > request.set_form_data({token: 'vjuri@mrd.com:CFTKGNZKXC'})
=> "application/x-www-form-urlencoded"
2.1.2 :032 > res = http.request(request)
=> #<Net::HTTPOK 200 OK readbody=true>
2.1.2 :033 > res.body
=> "{\"result\":{\"errorcode\":0,\"errormessage\":\"\",\"payments\":[]}}"

如何从 Controller 强制 Net::HTTP 工作?

最佳答案

这是 answer关于我的问题。
原因是

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.

关于ruby-on-rails - Net::HTTP::Get 超时问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33568775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com