This is my latest version, it includes quite a lot of commented out work in progress.
The error 500 can be caused by an extended ascii code, which I remove. Are there any errors returned by the node.js code? I suspect something copy/pasted badly.
//var utfstr = require(‘utf8’);
var http = require(‘http’);
var https = require(‘https’);
http.createServer(function(req, res){
main(req,res);
}).listen(1111);
function main(req, res){
let data = ;
req.on(‘data’, (chunk) => {
data.push(chunk);
});
req.on('end', () => {
data = Buffer.concat(data).toString();
// at this point, `data` has the entire request body stored in it as a string
console.log(' ');
console.log(' ');
console.log('Processing request');
console.log(data);
header = data.substr(26,43);
header = "--------------------";
data = data.substr(data.indexOf("name="));
data = data.substring(50,data.indexOf(header));
var data2 = '{"payload":' + data + '}';
// console.log(data2);
var jsondata2 = JSON.parse(data2);
// remove unwanted JSON items
// delete jsondata2.payload.Metadata[‘Role’];
// delete jsondata2.payload.Metadata[‘Similar’];
// delete jsondata2.payload.Metadata[‘Location’];
// delete jsondata2.payload.Metadata[‘summary’];
data2 = JSON.stringify(jsondata2);
data2 = data2.replace(/[^\x20-\x7E]/g, ‘’); // Remove non printable chars
//data2 = utfstr.encode(data2);
console.log(‘tidied data’);
console.log(data2);
const options = {
hostname: ‘graph-eu01-euwest1.api.smartthings.com’,
path: ‘/api/token/******************:’,
method: ‘POST’,
headers: {
// ‘Content-Type’: ‘application/json; charset=utf-8’,
// ‘Content-Disposition’ : ‘form-data; name=payload’,
‘Content-Type’: ‘text/plain;charset=UTF-8’,
‘Content-Length’: data2.length
}
}
//make request
const req2 = https.request(options, res2 => {
console.log(statusCode: ${res2.statusCode})
res2.on('data', d => {
process.stdout.write(d)
})
})
req2.on('error', error => {
console.error(error)
})
req2.write(data2,'UTF-8')
req2.end()
});
res.end();
}