Ruby 为什么可以';当使用smtpapi时,我不能让sendgrid替换模板中的变量吗?

Ruby 为什么可以';当使用smtpapi时,我不能让sendgrid替换模板中的变量吗?,ruby,sendgrid,Ruby,Sendgrid,我相信我已经遵循了SendGrid网站上的文档,但到目前为止,当我收到一封通过其API发送的电子邮件时,它从未替代我在x-smtpapi标题中指定的替代品。我使用HTTParty发送请求,如下所示: HTTParty.post(Sendgrid::Postman.api_url, { :query => params.merge({ "api_user" => @config[:api_user], "api_key" => @config[:api_key] }),

我相信我已经遵循了SendGrid网站上的文档,但到目前为止,当我收到一封通过其API发送的电子邮件时,它从未替代我在x-smtpapi标题中指定的替代品。我使用HTTParty发送请求,如下所示:

HTTParty.post(Sendgrid::Postman.api_url, { 
    :query => params.merge({ "api_user" => @config[:api_user], "api_key" => @config[:api_key] }), 
    :headers => headers, :format => :json
  })
{"from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo"}
{"X-SMTPAPI"=>"{\"sub\": {\"-first_name-\": [\"Foo\"], \"-email-\": [\"foo@gmail.com\"], \"-login-\": [\"heavysixer\"], \"-last_name-\": [\"Bar\"]}, \"to\": [\"foo@gmail.com\"]}"} 
params = {"api_user" => 'foo', "api_key" => 'bar', "from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo", "x-smtpapi"=>{"sub"=>{"-first_name-"=>["foo"], "-email-"=>["foo@gmail.com"], "-login-"=>["foo"], "-last_name-"=>["bar"]}}}

HTTParty.post(Sendgrid::Postman.api_url, :query => params, :format => :json)
“参数”如下所示:

HTTParty.post(Sendgrid::Postman.api_url, { 
    :query => params.merge({ "api_user" => @config[:api_user], "api_key" => @config[:api_key] }), 
    :headers => headers, :format => :json
  })
{"from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo"}
{"X-SMTPAPI"=>"{\"sub\": {\"-first_name-\": [\"Foo\"], \"-email-\": [\"foo@gmail.com\"], \"-login-\": [\"heavysixer\"], \"-last_name-\": [\"Bar\"]}, \"to\": [\"foo@gmail.com\"]}"} 
params = {"api_user" => 'foo', "api_key" => 'bar', "from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo", "x-smtpapi"=>{"sub"=>{"-first_name-"=>["foo"], "-email-"=>["foo@gmail.com"], "-login-"=>["foo"], "-last_name-"=>["bar"]}}}

HTTParty.post(Sendgrid::Postman.api_url, :query => params, :format => :json)
标题如下所示:

HTTParty.post(Sendgrid::Postman.api_url, { 
    :query => params.merge({ "api_user" => @config[:api_user], "api_key" => @config[:api_key] }), 
    :headers => headers, :format => :json
  })
{"from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo"}
{"X-SMTPAPI"=>"{\"sub\": {\"-first_name-\": [\"Foo\"], \"-email-\": [\"foo@gmail.com\"], \"-login-\": [\"heavysixer\"], \"-last_name-\": [\"Bar\"]}, \"to\": [\"foo@gmail.com\"]}"} 
params = {"api_user" => 'foo', "api_key" => 'bar', "from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo", "x-smtpapi"=>{"sub"=>{"-first_name-"=>["foo"], "-email-"=>["foo@gmail.com"], "-login-"=>["foo"], "-last_name-"=>["bar"]}}}

HTTParty.post(Sendgrid::Postman.api_url, :query => params, :format => :json)
邮件始终成功传递,但当邮件到达收件箱时,应替换的值仍然显示为-first_name-&-last_name-

我错过了什么?我一整天都在搞这个

----------------------------------------------------------- 更新: 根据下面的建议,我尝试将x-smtpapi参数推到表单帖子中,但我得到了相同的结果。我的帖子的查询字符串现在如下所示:

HTTParty.post(Sendgrid::Postman.api_url, { 
    :query => params.merge({ "api_user" => @config[:api_user], "api_key" => @config[:api_key] }), 
    :headers => headers, :format => :json
  })
{"from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo"}
{"X-SMTPAPI"=>"{\"sub\": {\"-first_name-\": [\"Foo\"], \"-email-\": [\"foo@gmail.com\"], \"-login-\": [\"heavysixer\"], \"-last_name-\": [\"Bar\"]}, \"to\": [\"foo@gmail.com\"]}"} 
params = {"api_user" => 'foo', "api_key" => 'bar', "from"=>"noreply@foo.com", "text"=>"Happy Holidays -first_name- -last_name-,\nI hope this message finds you in good health and high spirits.", "to"=>["foo@gmail.com"], "subject"=>"foo", "x-smtpapi"=>{"sub"=>{"-first_name-"=>["foo"], "-email-"=>["foo@gmail.com"], "-login-"=>["foo"], "-last_name-"=>["bar"]}}}

HTTParty.post(Sendgrid::Postman.api_url, :query => params, :format => :json)

他们的意思是x-smtpapi应该是发布的参数之一,而不是http头。

他们的意思是x-smtpapi应该是发布的参数之一,而不是http头。

如果您查看此文档,它看起来像是在指定一个头:这是他们的SMTP api。如果你看一下WebAPI的实际示例,它是一个参数(示例是用php编写的,但很明显发生了什么),我明白你的意思。我已经将“x-smtpapi”重新定位到查询参数中,但它仍然没有替换这些值。我觉得我遗漏了一些显而易见的东西,因为事实证明我需要替换一些生成的json以使其正常工作。然而你是正确的,它在文章中作为参数出现,而不是在标题中。非常感谢你的帮助!{'x-smtpapi'=>{'to'=>@params['to'],'sub'=>@params['sub']}.to_json.gsub(/([“]}])([,:])([“[”[{])/,'\\1\\2\\3')如果你看一下这个文档,它看起来像是在为他们的SMTP api指定一个标题。如果你看一下web api的实际示例,它是一个参数(示例是用php编写的,但发生的事情很明显)我明白你的意思。我已经重新定位了“x-smtpapi”到查询参数中,但它仍然没有替换值。我觉得我缺少了一些明显的东西。结果证明,我需要替换一些生成的json以使其正常工作。但是你是正确的,它在帖子中作为参数,而不是在标题中。非常感谢你的帮助!{'x-smtpapi'=>{'to'=>@params['to'],'sub'=>@params['sub']}。to_json.gsub(/([“]}])([,:])([“[{])/,'\\1\\2\\3')