Hello,
I have an API endpoint which has a bunch of optional parameters. I can either not include the parameter or send NULL. These parameters are a mixture of integers and strings. However, looking at…
It seems the webhook variables types are predefined when creating a webhook (i.e. quotes give a string and no quotes give an integer/float etc…). This means if I want to send NULL into a string variable, it gets cast as a string and if I want to send NULL as an integer it becomes a 0.
I also found GitHub - rickkas7/particle-webhooks: Particle Webhooks Intermediate Tutorial which talks about conditional blocks in the web hook’s body. I’ve tried this but so far I have not been able to get the conditions working. Has anyone been able to get optional parameters working with particle web hooks?
Sample of web hook configuration:
- Type: Post
- Format: JSON
- JSON Data:
{
{{{#variable1}}}
"variable1": "{{{variable1}}}",
{{{/variable1}}}
{{{#variable2}}}
"variable2": {{{variable2}}},
{{{/variable3}}}
{{{#variable3}}}
"variable3": "{{{variable3}}}",
{{{/variable3}}}
}
"version": {{{PRODUCT_VERSION}}}
Note variable1
and variable3
are strings and variable2
is an integer.
Sample publish code on firmware:
- Note I am using
JsonParserGeneratorRK
to generate the json
JsonWriterStatic<1024> body;
body.init();
body.startObject(); // Start 0
switch (mode) {
case 0: // Just send variable1
body.insertKeyValue("variable1", variable1);
break;
case 1: // Just send variable 2 and 3
body.insertKeyValue("variable2", variable2);
body.insertKeyValue("variable3", variable3);
break;
}
body.finishObjectOrArray(); // End 0
body.nullTerminate();
Particle.publish(webhook, body.getBuffer(), PRIVATE);
If I publish using mode 1
expect to publish just {"variable1":"variable1"}
, however on the backend I am getting…
{
"variable1": "variable1",
"variable2": ,
"variable3": "",
"version": 10
}
Just wondering if someone could point out what I am doing wrong.
I am on deviceOS 2.0.1 but I don’t think it should matter in this case.
A work around I have is to create multiple webhooks for the same endpoint. However this quickly becomes a hassel.