Webhooks - Custom Webhook

Hi,
I’m trying to use webhooks - custom webhook, like I used in v2 - basically thingsnetwork sends a request to a flask thing i’m running and I get the data that way. I’ve configured the webhook settings but no information is getting to my flask. Mqtt I can use in v3

My custom webhooks in V3 run. Note the data delivered differs in format from V2 and you need to specify when (uplink/join/…) the webhook will be called. Those changes might catch you out.

Right now I’m just trying to get any data transferred. Flask is listening to any kind of information, and it’s not getting anything from the webhook :s

EDIT : I just made it work, yay :smiley:

What was problem/solution so others may learn?

It’s not mandatory - the default is to use the base path for any message.

And my WebHooks are running beautifully too!

So you don’t enable anything? The text on the page suggests (to me) you need to enable a certain message type to receive it. It also allows specifying a custom path but that is optional. Might well be the wording is confusing me…

Shoot**, turns out I’ve got some with them all turned on and some with them all turned off.

My v3 config is work in progress so it’s not easy to quickly check - will do that later on this evening.

**Originally other words starting with B were here …

My V3 webhook is working, in that it triggers the code in my endpoint to log a visit, but it seems to arrive without any payload data. I’ve tried both specifying the message type and letting it send all message types. It is configured to send in JSON format which I attempt to recover using PHP like this:

header(‘Content-Type: application/json’);
$json = file_get_contents(‘php://input’);

But $json is empty.

It worked OK with V2 HTML integrations!

I don’t know what the effect of setting the header is at that stage, as it’s an outgoing response element.

The input is raw text, this works for me:

<?php
$data = file_get_contents("php://input");

// Save a raw copy of the message in a debug directory
// this will fill up quickly so don't leave it turned on
//$pathNFile = "debug/".date('YmdHis')."-".uniqid().".txt"; 
//file_put_contents($pathNFile, $data);

$json = json_decode($data, true);

...

Thanks for your help. I already do exactly as you suggest but even removing the header setting has no effect. Do you think that the problem could be related to my devices and applications being on V3 but the traffic is going via my TTN Indoor hub which is still on V2 because I have been unable to register it on V3?

Absolutely nothing to do with it at all.

If the traffic is appearing on the Application Live Data view it should then be forwarded.

You could turn on the save to file on the code snippet above so you can see data arriving (as files on the server).

What happens if you access your WebHook in your browser?

Hi Guys,
ME Too. Data arriving from devices on V2 to my end point URL no problem,

Devices on V3 with Webhook, The device is sending data to the V3 server and i can see the data on the V3 server, It is triggering the uplink and my end point URL is receiving the connection, but no body data i.e no json.

My apologies, I see that my WebHooks haven’t picked up any data since 10:18 today.

As per Heisenberg’s Uncertainty Principle, having poked at a few settings and copied them over on to another application, data is now appearing.

I’ll alert Head Office that there may be an issue.

Problem solved! It was nothing to do with TTN but I’ll detail the fix for anyone else who may have this problem.

The domain where my endpoint lives can either be found at https://mydomain.com or https://www.mydomain.com. Using the former involves a hidden redirection to the latter. In the process of redirection the payload is shed and the web hook arrives at its destination empty. Adding the full url to my webhooks solved the problem and I am now swimming in data.

I used the following code to test the endpoint:

<?php
// Not Heisenberg compliant
$url = '{your endpoint url}';
$ch = curl_init($url);
$data = array(
    'temperature' => '27.693',
    'humidity' => '40.773'
);
$payload = json_encode(array("user" => $data));

curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

print_r($result);

curl_close($ch);

?>

(Sorry I tried to put cr/lfs in the code - but simple things baffle me.)

3 Likes

Thanks for sharing the solution… just the kind of apparently minor thing that can lead to a big snafu for many…

Hi Guys,
I am still rather confused here.
I am still not sure as to the setting for the http integration. they seem pretty basic as on v2.
my devices still on v2 are sending data to my end point no problem. the devices i have change to v3 are sending data to the v3 server, visible in the live data, which says its sending the data by my url, but i am not receiving any body data i,e the json data. So i must have something wrong with my v3 settings if you are saying it does work. Again if i set the device back to v2 i get my data as expected.
so what am i missing.

It may be a bugette in the v3 stack - I went back in to my v3 WebHooks, ensured all the check boxes were checked and clicked Save. It wasn’t very scientific but it suddenly resulted in incoming payloads. Worth a try. Let us know how you get on and I can try & setup some testing if no joy.

Hi Nick, I have made a slight modification to the debug code that you posted, to provide a prettier output and a useful crib sheet for further coding.

<?php
//print("Start");	
$json = file_get_contents("php://input");
$data = json_decode($json, true);
ob_start(); //Start buffering
print_r($data); //print the result
$output = ob_get_contents(); //get the result from buffer
ob_end_clean(); //close buffer

// Save a raw copy of the message in a debug directory
// this will fill up quickly so don't leave it turned on
// Save a raw copy of the message in a debug directory
// this will fill up quickly so don't leave it turned on
$pathNFile = "debug/".date('Y-m-d H:i:s') .".txt"; 
file_put_contents($pathNFile, $output);
$fp=fopen($pathNFile,"a");
fwrite($fp,date('Y-m-d H:i:s'));
fclose($fp);
1 Like

Nice one!

For those coming along at a later date, this will return a response to a browser or any HTTP client type utility or command line curl request as @dhmitchell provided above.

It will not provide any benefit for deployment to be accessed by a WebHook as you never get to see the response. The stack doesn’t and can’t really do anything with a response.

Hi there,

OK ticked all my boxes in the enabled messages section, as yet no data is arriving.

but have noticed i have NO ‘downlink API key’ set i.e box is just blank.
On V2 i have it set to just default, could this have an effect?

Thanks for the help.