Webhook with ResponseTemplate times out

I’m trying to create my first webhook. I eventually want to pull some weather data from openweathermap.org. For now, I’m just starting simple and getting the current temperature.

Without a responseTemplate, my webhook works perfectly and the entire response from the API makes it into my eventhandler. When I add the responseTemplate, my eventhandler is never called at all. If I go into the Particle Console and click the test button for the webhook, it times out when I have the ResponseTemplate and works perfectly without it.

I’ve tried Rickkas7’s Moustache tester with my webhook and the response from the API, and it correctly pulls out the temperature.

In case it matters, I’m using a Photon with Device OS 1.4.4

My webhook is:

{
    "event": “JWeatherHook",
    "url": "http://api.openweathermap.org/data/2.5/forecast",
    "requestType": "POST",
    "noDefaults": true,
    "rejectUnauthorized": false,
    "responseTemplate": "{{main.temp}}",
    "query": {
        "appid": “<MY API KEY>“,
        "q": “<MY CITY>“
    }
}

Thank you for any help you can offer.

You do have some typographic double quotes () in there, these are not valid string delimiters - only standard double quotes (") are allowed.

(I have added a rendering hint to your post so you can better see the code hilighting as it would pertain to JSON format)

I didn’t even notice that. That’s from where I made a few edits in a text editor (which messed up those quotes) to remove personal info before pasting it into the forum. The quotes are all right in my console.

Your {{main.temp}} is wrapped inside an array list hence you need to enter into that array before retrieving any of its elements.

Try this response template instead

{{#list}} {{main.temp}} {{/list}}

Or a bit more elaborate

{ "temps": [ {{#list}} {{main.temp}}, {{/list}} "null" ] }
1 Like

That seems to have fixed the problem.
I don’t really understand it though. Do you know of a good tutorial. I don’t know how you knew it was an array and I don’t know enough about the language to have dealt with it with the list tags. It’s also going to be a challenge when I try to pull a lot more data than just a single field.

It did show another problem. Instead of returning 1 temperature, I’m getting about 20 in a space-separated list. When I look at the raw response in the console, it has the corresponding 20 complete blocks of data and the webhook is extracting the temperature from every one. I’m obviously doing something wrong with the API, and hopefully I can figure that one out, but the webhook appears to be working properly now, since it is the right answer for the data being returned.

Thank you for the help.

When you look at the raw response from openweathermap you will see something like this

{ ...., "list": [ ... ] ... }

These square brackets indicate a JSON array.

If you want a better structured response you can use my second (more elaborate) template, or you can limit the number of forcast responses by adding another query field for a limited count of results

    "query": {
        "appid": "<MY API KEY>",
        "cnt": "1"
        "q": "<MY CITY>"
    }

With “cnt”: “1” it’s doing exactly what I want. For some reason it’s working fine when I ask for the forecast but when I just ask for the weather it doesn’t like my webhook but works fine if I cut and paste the request into my browser.

I’ll probably just leave it this way since it’s giving me what I need now.

Not sure what you tried, but for me that works just fine too - just needs a different response template :wink:

BTW, I’d rather use a GET request when you want to get data. POST would primarily be for posting data to their servers.

Also you are currently requesting the temperature in Kelvin, but with another query field you can opt for Celsius ("units":"metric") or Fahrenheit ("units":"imperial").

I tried changing the “url” to “http://api.openweathermap.org/data/2.5/weather” with the same query parameters and that didn’t work.

I’ve never seen GET and POST described that way and it makes a lot of sense.

Thanks for the tip about units, I was going to just do the conversion on the Photon, but that makes it easier.

If I want to look at a specific result rather than the first one, is there a way to do that? I saw some references to something like main.0.temp, but when I try that I get an empty value returned.

Is there a size limit to how much will be sent to the photon? If I set the count higher and take a few fields, will the data the response handler on the Photon gets be truncated?

If the response would get too long for a single event, you’ll get multiple hook-response/JWeatherHook/0, hook-response/JWeatherHook/1 and so on. Your subscribe hander would need to catch them all and put them back together into a single result before parsing.

This webhook works fine for weather

{
    "event": "testWebhook",
    "url": "https://api.openweathermap.org/data/2.5/weather",
    "requestType": "GET",
    "noDefaults": true,
    "rejectUnauthorized": false,
    "responseTemplate": "{{main.temp}}",
    "query": {
        "appid": "<yourAppID",
        "q": "<yourCity>",
        "cnt": "1",
        "units": "metric"
    }
}

If you want a particular field of an array you need to index the array itself.
e.g. {{list.2.main.temp}}

Your webhook does work perfectly for me. Thank you.