Hi All,
I have a webhook setup in the integrations with custom JSON data that gets sent to the target endpoint.
{
"data": [
{
"name": "{{data.0.name}}",
"value": {{data.0.value}}
}
]
}
How do I pass dynamic value in Particle.publish() so the target url gets the dynamic value appended at the end (replacing the 1 in the url below)?
Webhook
URL: https://target.url/1
Device Application Firmware:
DynamicJsonDocument doc(1024);
// Create an array
JsonArray data = doc.createNestedArray("data");
// Create the first object in the array
JsonObject obj1 = data.createNestedObject();
obj1["name"] = "dataName";
obj1["value"] = dataValue;
// Allocate a buffer for the serialized JSON output
char jsonBuffer[1024];
serializeJson(doc, jsonBuffer, sizeof(jsonBuffer));
Particle.publish(eventName, jsonBuffer, PRIVATE);
@no1089 @rickkas7 @peekay123
Which dynamic value do you want at the end of the URL? {{data.0.value}}
? You can insert any of the mustache templates that you did in the body into the URL configuration.
Wait so just append {{data.1.value}}
at the end of the target url in the Webhook configuration and just pass whatever value I want in the custom JSON request when publishing?
Webhook Configuration Target URL: https://target.url/{{data.x.value}}
Device:
{
"data": [
{
"name": "{{data.0.name}}",
"value": {{data.0.value}}
},
{
"value": {{data.1.value}}
}
]
}
When the Webhook triggers, the target url becomes https://target.url/{{data.x.value}}?
You could also pass the value that goes in the URL at the same level as data
, so it wouldn’t confuse what is going into the body.
JSON sent from device:
{
"data": [
{
"name": "{{data.0.name}}",
"value": {{data.0.value}}
}
],
"url": 1234
}
The webhook configuration would be:
https://target.url/{{url}}
And then you could pass any data you want from the device in the URL (number or a string).
The backend expects this format:
{
"data": [
{
"name": "{{data.0.name}}",
"value": {{data.0.value}}
}
]
}
So doing this instead:
{
"data": [
{
"name": "{{data.0.name}}",
"value": {{data.0.value}}
},
{
"value": {{data.1.value}}
}
]
}
However the webhook is not triggering: https://target.url/{{data.1.value}}
Any suggestions?
What is the event you are publishing?
Do you mean what’s the event name?
Particle.publish("createReading", jsonBuffer, PRIVATE);
The JSON object being published (created using ArduinoJson community library).
{"data":[{"name":"x","value":0},{"value":"13"}]}
And you’re not getting a request at https://target.url/13
? What does the integration log indicate?
Okay, worked out the triggering issue.
The integration log now shows a failed request with 404 status code.
Is there a way to check that the target url is being set correctly with {{data.1.value}}
being appended at the end? I've double checked everything on the payload side and its the correct format.
Hi, I believe the integration logs can show you the URL.
This particular integration has nothing in it, but I've seen the URL in others.
1 Like