ruby-on-rails – SendGrid将多个电子邮件地址添加到Marketing Email API

我有一些真正难以尝试将json中的多个电子邮件地址发送到sendgrid API.

这是我的代码适用于一个电子邮件地址:

@result = HTTParty.post("https://api.sendgrid.com/api/newsletter/lists/email/add.json",
:body => { :list => "#{@survey.name}_#{@survey.id}", :data => '{ "name": "John Smith", "email": "john@smith.com" }', :api_user => 'XXXXX', :api_key => 'XXXXX'})

但是在使用:data post属性的ruby中,如何添加其他电子邮件地址?

以下不起作用 – 生活太容易了哈哈

:data => '[{"email" => "nick@sendgrid.com"},{"email" => "jane@example.com"}]'

要么

:data => '{[{"email" => "nick@sendgrid.com"},{"email" => "jane@example.com"}]}'

显然SendGrid期待以下….

data[]={"email" => "nick@sendgrid.com"}&data[]={"email" => "jane@example.com"}

我如何在rails中创建一个post参数呢?!?

最佳答案 您可以创建:data param作为JSON数组,如下所示:

:data => [‘{“name”:“Foo Bar”,“email”:“foo@bar.com”}’,'{“name”:“Jon Snow”,“email”:“jon@snow.com”}’ ]

点赞