Reputation: 3333
Trying to tun a simple chunk of code form index controller action.
But get Net::ReadTimeout error.
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: '[email protected]:CFTKGNZKXC'})
res = http.request(request)
render text: res.body
end
end
From rails console it works like a charm.
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: '[email protected]: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\":[]}}"
How to force Net::HTTP work from controller?
Upvotes: 2
Views: 797
Reputation: 3333
This is the answer on my question.
The reason is
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.
Upvotes: 1