我们有 2 个用 Rails 和 .Net Framework 4.8 编写的 REST Api 应用程序。我们从前端调用 rails api。对于某些服务,我们从 Rails API 项目中调用 .Net REST API。.Net API 项目部署到测试服务器。
奇怪的是,如果我从邮递员那里使用它们,我可以获得对 .Net Rest API 端点的响应。每当我尝试从 Rails API 项目调用 .Net Rest API 端点时,都会收到 OpenTimeout 异常。我尝试为打开超时设置不同的超时,但我不明白为什么它会抛出该错误。
我团队的其他人没有这样的问题。我是唯一面临这个问题的人。我确定问题出在 rails API 项目。但我找不到它是什么。
下面是我们用来发出 Http 请求的 CustomHttp 类。
require 'net/http'
require 'uri'
require 'json'
class CustomHttp
VERB_MAP = {
get: Net::HTTP::Get,
post: Net::HTTP::Post,
put: Net::HTTP::Put,
delete: Net::HTTP::Delete,
patch: Net::HTTP::Patch
}.freeze
def initialize(endpoint, token = '', client = nil, timeout = 240)
@endpoint = endpoint
@token = token
@base_url = '' # .Net Web API base url
@timeout = timeout
@http = client.blank? ? http_client(@base_url) : client
end
def get(path, params)
request_json :get, path, params
end
def post(path, params)
request_json :post, path, params
end
private
def http_client(base_url)
use_ssl = base_url.to_s.downcase.include?('https')
uri = URI.parse(base_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = use_ssl
http.read_timeout = @timeout
http
end
def request_json(method, path, params)
response = request(method, path, params)
body = JSON.parse(response.body, symbolize_names: true).with_indifferent_access
{
code: response.code,
body: body,
headers: response.instance_variable_get('@header')
}.with_indifferent_access
rescue JSON::ParserError => e
exception_parser(e, response)
rescue Timeout::Error => e
exception_parser(e, response)
rescue => e
exception_parser(e, response)
end
def exception_parser(e, response)
status_code = response.present? && response.code.to_i > 299 ? response.code.to_i : 500
error_message = !response.respond_to?(:message) || response.message.blank? ? e.message : response.message
{
code: status_code,
message: error_message,
body: {success: false},
headers: response.instance_variable_get('@header')
}.with_indifferent_access
end
def encode_path_params(path, params)
encoded = URI.encode_www_form(params)
"#{path}?#{encoded}"
end
def add_authentication(request)
req = request.clone
req['Authorization'] = "Bearer #{@token}" unless @token.blank?
req
end
def request(method, path, params = {})
req = http_verb_for(method, params, path)
final_req = add_authentication(req)
@http.request(final_req)
end
def http_verb_for(method, params, path)
case method
when :get
full_path = "#{@base_url}#{encode_path_params(path, params)}"
req = VERB_MAP[method.to_sym].new(full_path)
when :post_json
full_path = "#{@base_url}#{path}"
req = VERB_MAP[:post].new(full_path, 'Content-Type' => 'application/json')
req.body = params.to_json
else
full_path = "#{@base_url}#{path}"
req = VERB_MAP[method.to_sym].new(full_path)
req.set_form_data(params)
end
req
end
end
但是,下面的脚本有效。
require 'net/http'
AUTH_TOKEN = '' # place your auth token here
uri = URI.parse("Your endpoint to connect")
header = {'Content-Type': 'application/json', 'Authorization': "Bearer #{AUTH_TOKEN}"}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri, header)
http.open_timeout = 10
response = http.request(request)
puts response.body
不知道为什么我无法从 rails API 项目连接到服务器。
我们在 macOS中使用rails 5.0.7
, 。如果我能解决这个问题,那将会很有帮助。ruby-2.5.3
Catalina