When creating a custom webhook endpoint, the value {{SPARK_EVENT_VALUE}} gets HTML encoded so the endpoint receives " instead of proper quotation marks, making JSON decoding fail.
I created a webhook with this format:
{
"eventName": "hm_",
"url": "http://myserver.com/device_events",
"requestType": "POST",
"json": {
"name": "{{SPARK_EVENT_NAME}}",
"value": "{{SPARK_EVENT_VALUE}}",
"occured_at": "{{SPARK_PUBLISHED_AT}}",
"device_id": "{{SPARK_CORE_ID}}"
},
"mydevices": true
}
When I publish an event from the Spark with the data formatted as JSON, the value
field posted to my endpoint is HTML encoded.
This is a raw HTTP request that was posted to my endpoint. Notice how the original data
field is correctly quoted, but the value
field is not.
POST /device_events HTTP/1.1
User-Agent: SparkBot/1.0 (http://docs.spark.io/webhooks#bot)
host: redacted
accept: application/json
content-type: application/json
content-length: 312
Connection: keep-alive
{"event":"hm_data","data":"{\"battery\":4.896004,\"hours\":0.733333}","published_at":"2015-06-03T18:23:24.786Z","coreid":"redacted","name":"hm_data","value":"{"battery":4.896004,"hours":0.733333}","occured_at":"2015-06-03T18:23:24.786Z","device_id":"redacted"}
The firmware code that published this event is:
String data = String("{"
"\"battery\":" + String(battery) + ","
"\"hours\":" + String(hours) +
"}");
Spark.publish("hm_data", data, 60, PRIVATE);
Is this a bug in the server code?
Also, I expected that when I provide a custom “json” field in the webhook then the default fields would be omitted. What if my endpoint throws an error when receiving unknown fields? Adding an option to the webhook config file “omitDefaultFields” would allow to more completely customize the webhook request.