4

I'm having some real difficultly trying to send multiple email addresses in json to the sendgrid API.

Here is my code that works for one email address:

@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'})

But in ruby using the :data post attribute how can I add another email address?

The following don't work - life would be too easy lol

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

OR

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

Apparently SendGrid expects the following....

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

How I can create a post parameter in rails that does this?!?

David
  • 3,166
  • 2
  • 30
  • 51

1 Answers1

2

You could create your :data param as an array of JSONs, like so:

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