Webhook with multiple variables with the same name

Mailgun, fore example, expects multiple “to” keys in the json payload for batch sending emails to multiple recipients via a single API call.
But Particle appears to overwrite key:value pairs if the same key is used multiple times.
See example below…
Anyway around this?

JSON:

{
    "event": "mailgun",
    "url": "https://api.mailgun.net/v3/sandboxXXXXXXXXXXXXXX.mailgun.org/messages",
    "requestType": "POST",
    "mydevices": true,
    "auth": {
            "username": "api",
            "password": "key-XXXXXXXXXXXXXXXXXXXXxxxx"
            },
    "noDefaults": true,
    "form": {
            "to": "mary@gmail.com",
            "to": "joe@gmail.com",
            "from": "Mailgun < mailgun@sandboxXXXXXXXXXXXXXXXXX.mailgun.org >",
            "subject": "{{s}}",
            "html":"{{1}}"
            }
 }

Result:

% particle webhook create mailgun.json

Using settings from the file mailgun.json
Sending webhook request  { uri: '/v1/webhooks',
  method: 'POST',
  json:
   { event: 'mailgun',
     url: 'https://api.mailgun.net/v3/sandboxceXXXXXXXXXXXXXXXX.mailgun.org/messages',
     deviceid: undefined,
     requestType: 'POST',
     mydevices: true,
     auth:
      { username: 'api',
        password: 'key-XXXXXXXXXXXXXXXXXXXXXX },
     noDefaults: true,
     form:
      { to: 'Joe < joe@gmail.com >',
        from: 'mailgun < mailgun@sandboxceXXXXXXXXXXXXXXX.mailgun.org >',
        subject: '{{s}}',
        html: '{{1}}' } },
  headers: { Authorization: 'Bearer XXXXXXXXXXXXXXXXXXXXXX' } }
Successfully created webhook with ID XXXXXXXXXXXXXXXXXXXXX

Interesting question! So, JSON doesn’t let you set duplicate properties like this, they need to be unique for a given scope.

It looks like there is an easy workaround, just put all your email addresses in one “to” property, and separate them with commas. :slight_smile:

I hope that helps!

Thanks,
David

1 Like

I think I tried the comma trick, but had an issue with the format “bob < bob@gmial.com >, jim jim@gmail.com”. Should work for "bob@gmail.com,jim@gmail.com".

At any rate, it’s not a JSON issue, but how Particle parses it, right?
Mailgun accepts multiple “to” keys in the payload.

Thanks for the reply, Dave.

Hi @gcollins,

In this case the JSON standard says they should be unique, and if they’re not the behavior is unpredictable.

https://tools.ietf.org/html/rfc7159#section-4

Your request is a “form” request, so you can pass multiple properties with the same name, but in this case you’re encoding a form request in a JSON object, so JSON rules apply.

Thanks!
David

2 Likes

The Standard you quote does mention that parsing libraries may report an error, or the last value, or all the name/value pairs including duplicates. The latter would be best in the case of mailgun, since it is the software receiving the non-unique objects and it knows how to handle them.

Again, thanks for the thorough explanation.

Gus