Webhook data retrieval from Photon in "Headers" section

Is it possible to use mustache notation inside a webhook in order to put a string received from a Photon into the “Headers” section of my webhook? The Photon is sending up something that looks like this as the data during particle.publish()

{
   "access_token": "NgA6ZcYI...ixn8bUQ",
   "token_type": "Bearer",
   "scope": "user-read-private user-read-email",
   "expires_in": 3600
} 

and I’m trying to access the access_token and put it into my “Authorization” header slot like this:

where I’ve tried using {{PARTICLE_EVENT_VALUE.access_token}} and also {{access_token}} among many other attempts. These ones make the most sense to me though, but I’m not having any success and was just wondering if I’m restricted from doing this in this section of the webhook or something? Or maybe it has to do with trying to include the "Bearer " at the beginning but I didn’t think this would matter since the mustache template seems to just be direct text substitution. Also is there any way to view what that substitution value ends up being after the substitution before the request is sent? That would definitely aid my debugging process. Thanks in advance!

{{access_token}} should work in the HTTP headers.

Make sure the string isn’t getting truncated. The publish size limit is 255 characters, so if you exceed that the end of the data will be missing, the JSON will be invalid, then no mustache templates are expanded.

There are some more tips in my intermediate webhook tutorial.

You may find using RequestBin to be helpful, as well as the mustache tester in the document.

3 Likes

@rickkas7 Thanks! I found your mustache tester and that is very helpful to see. I will look into RequestBin as well. The access token that is sent is usually about 144 characters which fits but the rest of the object (which I don’t need) might put it over 255. However, I’m able to read out the whole object with all data fields intact on the online console:

Does this mean it is intact or can I not trust that?

A mustache template can be used in the headers.

Here’s my webhook:

{
    "event": "bearer",
    "url": "https://requestb.in/13i6i5u1",
    "headers": {
        "Authorization":"Bearer: {{token}}"
    },
    "requestType": "POST",
    "json":{
        "test":"{{value}}"
    },
    "mydevices": true,
    "noDefaults":true
}

I triggered it from the CLI:

particle publish bearer "{\"token\":\"abc134\",\"value\":\"xyz\"}"

And I got this is request bin:

1 Like

If you can see the whole value in the console it’s not being truncated, so that’s probably not the problem, though I’m not sure what is.

1 Like

@rickkas7 Thanks very much! The example was helpful and RequestBin turned out to be super helpful, definitely a useful resource. It turns out you were right originally, the response object was being truncated (despite being shown fully intact on the console when the photon sends its data response, not sure why) so I’m assuming that because it wasn’t terminated properly (i.e } ) the mustache template’s access attempt did not work because the data wasn’t a recognized as a full object, technically probably just a string. So I just parsed out the part I needed in the firmware on the photon and then just sent that up and it worked :smile:

1 Like