【发布时间】:2021-05-22 10:00:08
【问题描述】:
我正在尝试从我们的应用 ping 我们的 Rails 微服务之一(处理电子邮件),但我收到标题错误。
我们的微服务在 localhost:3101 上的 docker 中运行,而我们的应用程序也在 docker 上的 localhost:3000 上 - 我已经用邮递员请求对其进行了 ping 操作,它工作正常。
我在 ENV 变量中设置了我们的身份验证令牌和电子邮件服务 API,如下所示:-
EMAIL_SERVICE=http://localhost:3101
EMAIL_SERVICE_TOKEN=1234
这是我的EmailService:-
class EmailService
require 'net/http'
HEADER = { 'Content-Type': 'application/json' }.freeze
ENDPOINTS = {
root: ENV['EMAIL_SERVICE'],
test_email: {
post: {
url: '/api/test_email'
}
}
}
def initialize
@token = ENV['EMAIL_SERVICE_TOKEN']
end
def call_api(section, action, data, method='POST')
address_url = ENDPOINTS[section][action][:url]
uri = URI.parse("#{ENDPOINTS[:root]}#{address_url}")
# Create the HTTP objects
http = Net::HTTP.new(uri.host, uri.port)
if method == 'POST'
request = Net::HTTP::Post.new(
uri.request_uri,
HEADER
)
request.body = data.merge({ auth_token: @token }).to_json
else
request = Net::HTTP::Get.new(
uri.request_uri,
HEADER
)
end
http.use_ssl = true
response = http.request(request)
end
end
知道我错过了什么吗?
【问题讨论】:
标签: ruby-on-rails ruby docker microservices net-http