I am working in a Particle webhook which has a JSON that could have a lot of data and I plan to provide through query parameters the id of the correspondent array of data that I want to collect.
However, I’m having problems getting the correct mustache template/response template format correct.
I’ve been testing the URL giving the query parameters over postman and everything works good but when I try to add a filter to the query parameters, it simply looks like I am doing it in a wrong way.
I have three query parameters.
id
access_token
filter
Here’s an example from postman and the response. The number 14 is provided by me and it will be provided later by the Particle Photon.
So basically what I want is to figure it out is to know how to setup filters over the query parameters in particle webhooks to get the response that I want (Like the postman picture). This could benefit people that want to handle dynamic JSON data. Thanks!
Why do you have leading double quotes before the opening curly braces?
You don’t have them in your postman JSON, so why add it then in your code?
Have a close look and compare the working and non-working strings and see
working
not working
BTW, the number of opening double quotes does not match the number of closing ones either.
You reference a {value_id} (which should actually be wrapped in double or tripple curly braces) but your data string does not contain such a key - how would the webhook/integration know what value to use?
I'm also somewhat confused about your actual data flow and also some of your terminology (e.g. response template vs. custom request body)
Could you explain
what data (without any "decoration" or formatting) you want your device to send to the webhook
what the webhook should then send to your target server (formatted as the server expects to see it)
what you expect to get back from the server to the webhook (formatted as is)
what you want to get from the webhook delivered back to your device (this is what the response template defines)
(the first two bullets refer to the request while the latter two refer to the response)
Could you also post the webhook definition (via the CUSTOM TEMPLATE tab in the integrations edit page).
I think you are overcomplicating the matter. Webhooks/integrations are meant to add some level of abstraction between the device and the target server and hence reduce complexity of the request message the device has to create.
Your device should only need to care about the most important data while the webhook/integration takes care of properly formatting the request.
possible solution
If you only want to send userID and workoutID to the webhook and want the webhook to wrap the workoutID into your request variable you could create a simple JSON on the device like this
char reqData[64];
snprintf(reqData, sizeof[reqData], "{ \"userID\": \"%d\", \"woID\": \"%d\" }", my_id, my_workoutId);
// expected format: { "userID": "123", "woID": "456" }
Serial.println(reqData); // always check how the string looks on the device ;-)
Particle.publish("workout_rest", reqData, PRIVATE);
and reference these values in the webhook like this
That is the beauty of webhooks, your request string from the device to the cloud doesn't need to know anything about the actual format of your request towards the server. Your device only sends the dynamic data which then gets inserted into the request template according to the format the server requires.
For maintenace and readability it's also good to use different names/keys so that you can distinguish which is the incoming and what the outgoing data (e.g. incoming "userID" goes out as "id" and incoming "woID" goes out as "workoutId").
I was definitely confused about how data flow should be sent to the cloud.
Your proposed solution works!!!
Yes, I was basically overcomplicating the matter, I thought that I had to send to the particle cloud the same formatted data as it was in the query parameters defined.
Thank so much for your time and also for the tips!