How best send multiple data points with webhook?


#1

The docs for webhooks is distributed between Particle Guides, Particle Reference | Cloud, and Particle Reference | Firmware so I may have missed something important.

I am wondering what is the best way to send several data parameters in one webhook event. Looking at the Particle.publish() call, it seems that the “data” parameter is a single String object.

Using the Particle Dashboard Integrations, one can set up a “Form” type webhook and enter many parameters. However, I am only aware of one “metavalue” to reference the “data” parameter in the publish() call – that metavalue is: “{{PARTICLE_EVENT_VALUE}}”.

So it seems that only one form parameter is needed to carry dynamic data from the Electron device. That is convenient for reporting the value of a single sensor, for example, but makes it more complicated to send values from multiple sensors.

I could build my own string with a format for several parameters, such as a JSON string and put it in the data parameter, but that seems counter to the spirit of “ease of development” that Particle promotes.

I am wondering if there is some other protocol, such as passing an array of string values to the publish() call, which might then be bound in the same order to some form parameters set up in the same order, each having a value defined as: {{PARTICLE_EVENT_VALUE}}.

But I don’t see such a feature. Is there any convenient way to set up a form webhook to pass multiple dynamic data values?


#2

Yes, you can send multiple data values in a Particle publish if you format the event data as JSON. Say you want to send 2 integers, a and b, and a string c. You might send up something like this in the event data.

{"a":12,"b":34,"c":"testing"}

Then, instead of using {{PARTICLE_EVENT_VALUE}} you can use {{a}} or {{b}} or {{c}} in your webhook definition. It works in url, query parameters, JSON post data, and form post data.

There are even more complicated scenarios using nested object and arrays, but just that much is quite powerful.

My tutorial:


#3

Thanks, I’ll look at your tutorial.


#4

Sorry there isn’t an easier to use API, but the [SparkJson] (https://github.com/menan/SparkJson) library is pretty good. It’s in the community libraries if you’re using Particle Build (web IDE). Though a lot of people just brute-force it using sprintf as well.


#5

I haven’t mastered your tutorial yet, but one question: once I’ve formatted a JSON string and passed it to the publish() method as the data parameter, why bother to set up the individual parameters in the web hook? Isn’t it just as easy to receive the JSON string as a single parameter (“data”) in the web service and parse the string for the parameters there? Or is there some advantage to breaking out the parameters before transmitting to the web service? I’m probably not clear on the http request that actually is sent by the web hook in both cases.

-SB


#6

If you have the receiving server in your own hands, that’s possible and you are free to choose any format you like, but webhooks are mainly targeted at 3rd party servers and these don’t like arbitrary formats but prefere standards.
And JSON is very flexible and hence widely adopted on these 3rd party servers.


#7

At this time, you need to break out the individual fields you want to pass in whatever thing you want to pass them in, such as query parameters, JSON post data, or form post data. Like the {{a}} values in my example above.

It’s more flexible, and a little more complicated, and, unfortunately, it’s currently the only way it works the way you probably want it to.

The problem is that {{PARTICLE_EVENT_VALUE}} is always treated as a string. Thus if you try to pass multiple JSON values encoded that way, the receiving server will see them as one big string, not separate JSON values. If you have complete control over the server, you might be able to work around this, but probably not if you’re using a 3rd party service.


#8

Thanks ScruffR and rickkas7 – that clarifies the motivation when to break out the parameters. In our case, we do possess the receiving server, but knowing how to set the parameters is very valuable for other uses.

Regards,
-SB


#9

I am new to web hooks. I want to send a “block” of records at each publish that hook to Grovestreams.

My data is like this
Date : String, Time : String, Event ID : uint8, TagID : unit16, Vehicle ID: uint16, Param1 : uint8, Param2 : unit8

I may have anywhere from 1 to 20 records per publish.

Do I just generate a Json Array and publish the array?

Grovestreams have customizable API, Maybe the array can be sent as a single large string and decoded on their site?

Also, I am using Dev IDE on my desktop and cannot get the “Add to Project” option when I attempt to “USE” the library, I only get option to “create new project”, anyone know how to get the “Add to project” option?

Thanks


#10

I guess this applies here too