Webhook replacement variables

First, thanks for the previous help provided on using Web hooks. I have my code functioning and now want to clean up some of the processes to provide the web hook body from firmware instead of the integration form.

I currently have a ‘POST’ web hook with the following custom body:

{
  "location": {
    "address": {
      "line1": "351 N Main",
      "line2": "none",
      "city": "Nolos",
      "state": "OH",
      "zip": "44122"
    }
  },
  "services": {
    "fire": false,
    "police": false,
    "medical": false,
    "other": true
  },
  "name": "Nivlek Trahreg",
  "phone": "14444470569",
  "pin": "12234"
}

How I want to use a web hook where I can provide the data from firmware. So I tried this:

{
  "location": {
    "address": {
      "line1": "{{{line1}}}",
      "line2": "{{{line2}}}",
      "city": "{{{city}}}",
      "state": "{{{state}}}",
      "zip": "{{{zip}}}"
    }
  },
  "services": {
    "fire":  "{{{fire}}}",
    "police": "{{{police}}}",
    "medical": "{{{medical}}}",
    "other": "{{{other}}}
  },
  "name": "{{{name}}}",
  "phone": "{{{phone}}}",
  "pin": "{{{pin}}}
}

The code I used is

 testData = String::format( "{ \"line1\":\"%s\",\"line2\":\"%s\",\"city\":\"%s\",\"state\":\"%s\",\"zip\":\"%s\",\"fire\":\"%s\",\"police\":\"%s\",\"medical\":\"%s\",\"other\":\"%s\,\"name\":\"%s\",\"phone\":\"%s\",\"pin\":\"%s\"}",  line1.c_str(), line2.c_str(), city.c_str(), state.c_str(), zip.c_str(), (fire ? "true" : "false"), (police ? "true" : "false"), (medical ? "true" : "false"), (other ? "true" : "false"), name.c_str(), phone.c_str(), pin.c_str());

Particle.publish("498 testData", testData);
delay(10000);
Particle.publish("nla_alarm_create", testData, PRIVATE);     
delay(60000);

The testdata looks good but the POST returns a 400 error.
What am I missing?
Thanks.
5310

First I’d like to remind you of the reference documentation
https://docs.particle.io/reference/device-os/firmware/#particle-publish-

which appears to be ignored in this code

Next, I have reformatted your post so that code blocks are properly formatted (by means of the preformatted text feature image) which reveals that your webhook definition is missing several closing double quotes.

What ScruffR said. Also, there’s a mustache template tester in the docs that will preview exactly what will be sent and validate the resulting JSON if you’re generating JSON.

https://docs.particle.io/tutorials/device-os/json/#try-it-out

You sure do have a toxic way of helping but thanks anyways. If I was perfect I would be asking. In the end, those fixes didn’t change anything.

:thinking:
my English is bad bad but what did you want to express I guess that even the oldest highlanders didn’t know.

the Help doesn’t mean the “solution on the plate” which in this case appears that any way you get it :+1: from both @ScruffR and @rickkas7

I don’t use Web hooks anywhere but thanks to @rickkas7 link it took me 7 minutes to figure out how you should formatted your Web hooks

remove the space from your event name

missing closing double quote

missing closing double quote.

which fixes ??? nobody fix nothing here just point you from where to start !

Best Regards

4 Likes