auths
objectThe target URL for the proxied payload. For testing, https://requestbin.com and https://beeceptor.com are great resources.
return
or this.key = 'value'
, pass input data to your code viaparams
, and maintain state across executions with$checkpoint.async
(event, steps, params) => {
}
const axios = require('axios');
const { headers, body } = event;
const { authorization, targetUrl } = params;
if (authorization && authorization !== headers.authorization) {
await $respond({
immediate: true,
status: 401,
body: 'Missing or incorrect Authorization header in request'
});
$end('Missing or incorrect Authorization header in request');
}
console.log('Got body', body);
// See https://www.thethingsnetwork.org/docs/applications/http/
// When using a Decoder in the application's Payload Formats in
// TTN Console, then body.payload_fields will be available too.
const bytes = Buffer.from(body.payload_raw, 'base64');
// Assuming a Kickstarter The Things Node, with the default sketch;
// see https://www.thethingsnetwork.org/docs/devices/node/
const events = {
1: 'setup',
2: 'interval',
3: 'motion',
4: 'button'
};
const decoded = {};
decoded.event = events[body.port] || 'unknown';
decoded.battery = (bytes[0] << 8 | bytes[1]) / 1000;
decoded.light = bytes[2] << 8 | bytes[3];
// Sign-extend to 32 bits to support negative values, by shifting
// 24 bits to the left (16 too far), followed by a sign-propagating
// right shift of 16 bits, to effectively shift 8 bits:
decoded.temperature = (bytes[4] << 24 >> 16 | bytes[5]) / 100;
decoded.comment = 'Decoded and enhanced in pipedream workflow';
const options = {
url: targetUrl,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
data: decoded
};
console.log('Will POST', options);
// Rather than using axios, one could also use $send.http (which would
// send fully asynchronously, after the workflow runs, and would not
// count against workflow quota)
const response = await axios(options);
const result = `Proxied to ${options.url}, ${response.status} ${response.statusText}`;
console.log(result);
// The HTTP Integration doesn't really care about the response
await $respond({
immediate: true,
status: 202,
body: result
});