So I'm new to JavaScript programming. I'm getting this JSON payload on a POST request:
{
"subscriptionId" : "asdasdasdasd",
"originator" : "localhost",
"contextResponses" : [
{
"contextElement" : {
"type" : "",
"isPattern" : "false",
"id" : "id",
"attributes" : [
{
"name" : "temperature",
"type" : "int",
"value" : "5"
}
]
},
"statusCode" : {
"code" : "200",
"reasonPhrase" : "OK"
}
}
]
}
And using this code to try and access a certain field:
var qs = require('querystring');
var http = require('http');
http.createServer(function (req, res) {
var obj;
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
console.log(body.attributes.value);
if (body.length > 1e6)
req.connection.destroy();
});
req.on('end', function () {
var post = qs.parse(body);
// use post['blah'], etc.
});
}
}).listen(8087, "188.???.??.???");
console.log('Server running at http://188.???.??.???:8087/');
As you can see I'm trying to access the value field and trying to retrieve the number 5. Obviously, it's not working. I tried googleing it and this is probably something really silly. Any suggestions on how to access the field?
EDIT:
The data from console.log(body):
Server running at http://????????
{
"subscriptionId" : "asdasdasdasd",
"originator" : "localhost",
"contextResponses" : [
{
"contextElement" : {
"type" : "",
"isPattern" : "false",
"id" : "fiwaresensorfinal",
"attributes" : [
{
"name" : "temperature",
"type" : "int",
"value" : "5"
}
]
},
"statusCode" : {
"code" : "200",
"reasonPhrase" : "OK"
}
}
]
}
The console.log(data):
<Buffer 7b 0a 20 20 22 73 75 62 73 63 72 69 70 74 69 6f 6e 49 64 22 20 3a 20 22 35 35 38 31 37 62 33 62 39 38 61 64 64 31 38 63 63 33 65 31 38 33 62 65 22 2c 0a ...>
The console.log(recv):
{ subscriptionId: '55817b3b98add18cc3e183be',
originator: 'localhost',
contextResponses: [ { contextElement: [Object], statusCode: [Object] } ] }