Particle.publish() to trigger webhook BUT pass in a dynamic value to be appended at the end of the target endpoint url

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);

No, the actual raw JSON.

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

It shows the host domain only not the path :frowning:

can you show a screenshot of your webhook, around the full URL field?

No idea.
I would try using a remote endpoint to test all is working, like https://pipedream.com/

I would place the {{data.8.value}} in the body of the message that I send to that pipedream.com webhook so I can see its value as well.

cheers
(if you did something like this already, please disregard)

Is the dot between data.8 and value in the URL cut off in the screenshot or is it actually missing?

2 Likes

That's got to be one of the silliest mistakes Lol
Good eye @ScruffR works now, thank you!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.