We have 2 REST Api applications written in Rails and .Net Framework 4.8. From the front-end we call rails api. For some services we call .Net REST API from Rails API project. .Net API project is deployed to test server.
Odd thing here is, I can get response for .Net Rest API endpoints if I consume them from postman. Whenever I try to call .Net Rest API endpoints from Rails API project, I'm getting OpenTimeout Exception. I tried setting up different timeouts for open-timeout, But I'm not getting why it throwing that error.
Others from my team has no such problem. I'm the only one facing that problem. I'm sure that the issue is from rails API project. But I couldn't find what it is.
Below is the CustomHttp Class we use to make Http requests.
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
But, Below script worked.
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
Don't know why I can't connect to the server from the rails API project.
We are using rails 5.0.7
, ruby-2.5.3
in macOs Catalina
.
It'll be helpful If I can get a fix for this.