Webhook with integers and floats

Is there any way to create a custom json body in the webhook that contains different types besides strings ?

{
   "data": {
        "lat": "{{{lat}}}",
        "lng": "{{lng}}"
    },
    "published_at": "{{PARTICLE_PUBLISHED_AT}}",
    "coreid": "{{PARTICLE_DEVICE_ID}}"
}

But lat and lng are floats. I read at the reference guide that it doesn’t support other type than string. Is this true ?

If it’s true, then I have to deal with the types on my server right ?

Thanks.

It’s now possible to use the Console to define hooks with integers values. See Console webhook editor improvements

Under the covers, it send the json property as a string. As long as after replacing {{PARTICLE_EVENT_VALUE}} and other variables the string ends up being valid JSON it will work.

1 Like

Thank you, I’m going to check it out !

@rickkas7 has a good tutorial using Google Firebase. https://github.com/rickkas7/firebase_tutorial

Towards the end of the tutorial he explains how to use the Webhook body feature, which allows you to send JSON as a number instead of string.

Example JSON code:

{
    "event": "test1data",
    "url": "https://test1-c0b7e.firebaseio.com/test1data/data.json",
    "requestType": "POST",
    "headers": {
    	"Content-Type":"application/json"
    },
    "query": {
    	"auth":"vuZ30Pobxoq8BwfYuzrQJjxqMq7F2HRPUBt0FOyY"
    },
    "body":"{\"a\":{{a}}, \"b\":{{b}}, \"c\":{{c}}, \"ts\":\"{{PARTICLE_PUBLISHED_AT}}\" }", 
    "mydevices": true,
    "noDefaults": true
} 

The secret is in the syntax used in the JSON. For example, a, b, and c are numbers and ts is a string.
{{a}} would give you a number.
"{{a}}" would give a string.

And of course, you would upload this webhook using particle CLI:

particle webhook create name.json

Good luck!

EDIT: totally skipped over @jvanier post. That is awesome that you’ve made improvements to the Console. Well needed, thanks!

You can now also do this:

{
    "event": "test1data",
    "url": "https://test1-c0b7e.firebaseio.com/test1data/data.json",
    "requestType": "POST",
    "query": {
    	"auth":"vuZ30Pobxoq8BwfYuzrQJjxqMq7F2HRPUBt0FOyY"
    },
    "json": "{\n    \"a\":{{a}},\n    \"b\":{{b}},\n    \"c\":{{c}},\n    \"ts\":\"{{PARTICLE_PUBLISHED_AT}}\"\n}",
}

In fact, if you paste this in the Custom Template tab of the new webhook screen in the Console and go back to the Webhook Editor, you’ll get a nice JSON editor for your custom data.

1 Like