今天,我有很多挫伤,试图一次学习两件事...用于Postmark和Rails HTTP请求的API。
目标:使用Heroku的Postmark附件发送生产电子邮件。
我正在尝试将有关HTTP请求的这篇文章... http://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html ...与该API标记邮戳... http: //developer.postmarkapp.com/developer-send-api.html
不幸的是,Postmark中的示例是在curl中完成的,而我没有成功将它们转换为HTTP请求。我怀疑问题出在排头附近-变速箱的除车身以外的部分。
在下面的代码中看到的抢救子句将捕获错误“对等方重置连接”。此时,我不知道我是否接近提供Postmark身份验证的标头的正确格式。
我有正确的服务器令牌(在config条目中),“发件人”电子邮件已获得必需的邮戳签名。
def send_production_email(email_address, subject, email_body)
# Use API to interact with Heroku add-on Postmark
# http://developer.postmarkapp.com/developer-send-api.html
uri = URI('https://api.postmarkapp.com/email')
# Form the request
req = Net::HTTP::Post.new(uri)
# Set request headers -- SUSPECT THIS IS WRONG
req['Accept'] = 'application/json'
req['Content-Type'] = 'application/json'
req['X-Postmark-Server-Token'] = Rails.application.config.postmark_token
rbody ={
'From' => 'Support <[email protected]>',
'To' => email_address,
'Subject' => subject,
'HtmlBody' => wrap_html(email_body),
'TextBody' => email_body
}.to_json
req.body = rbody
# Send the request, waiting for the response
begin
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
rescue Exception => e
logthis("http request error: #{e.message}")
return
end
# ...parsing section omitted since I do not get that far...
end
再次尝试以这种方式格式化,但会导致相同的对等方重置错误:
rbody ={
'From' => 'Support <[email protected]>', # TODO: replace email when domain is live
'To' => email_address,
'Subject' => subject,
'HtmlBody' => wrap_html(email_body),
'TextBody' => email_body
}.to_json
uri = URI('https://api.postmarkapp.com/email')
http = Net::HTTP.new(uri.host, uri.port)
# http.use_ssl = true
request = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Postmark-Server-Token' => Rails.application.config.postmark_token})
request.body = rbody
# Send the request, waiting for the response
begin
response = http.request(request)
rescue Exception => e
logthis("http request error: #{e.message}")
return
end
感谢您的指导!
我是Wildbit的雇员,也是Postmark Ruby官方gem的维护者。
“对等连接重置”错误是您尝试将未加密的HTTP请求发送到希望通过HTTPS进行安全通信的端点的结果。因此,如果更改此行:
Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
到:
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.start { |http| http.request(req) }
那么您应该能够从API收到响应。我看到您在第二个示例中有此行,但已对此进行了注释。由于您是在练习,所以我想补充一点,在使用时,net/http
您通常不必使用诸如之类的基础类Net::HTTP::Post
。通常使用Net::HTTP
类实例提供的更高级别的API更简单。这是使用方法可以简化您的方法的示例:
def send_production_email(email_address, subject, email_body)
uri = URI('https://api.postmarkapp.com/email')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
headers = {'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Postmark-Server-Token' => Rails.application.config.postmark_token}
payload = {'From' => '[email protected]',
'To' => email_address,
'Subject' => subject,
'HtmlBody' => email_body,
'TextBody' => email_body}
http.post(uri.request_uri, payload.to_json, headers)
rescue => e
puts "http request error: #{e.message}"
end
并且,如果您net/http
对正式的Postmark Ruby gem的用法感兴趣,请查看HttpClient类的源代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句