Data Query NODE JS


#1

Good night I can make an appointment in Thinger.io using some kind of programming NODEJS or PHP


#2

Hi @Rodrigo_Cardoso_da_l, I think I don’t understand you. Do you mean interacting with your devices from Node.JS or PHP?


#3

sorry for my English. Yes I want to interact with NODE JS


#4

Hi,

here you have an example for posting data to a resource:

var http = require("http");
var body = '{"in": true}';
var options = {
  hostname: 'api.thinger.io',
  port: 80,
  path: '/v2/users/<username>/devices/<device_id>/<resource_name>',
  method: 'POST',
  headers: {
      'Authorization': 'Bearer <device_token>',
      'Accept': 'application/json, text/plain, */*',
      'Content-Type' : 'application/json;charset=UTF-8',
      'Content-Length': Buffer.byteLength(body)
  }
};
var req = http.request(options, function(res) {
  console.log('Status: ' + res.statusCode);
  console.log('Headers: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (body) {
    console.log('Body: ' + body);
  });
});
req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});
// write data to request body
req.write(body);
req.end();

And other example for getting data from a resource:

var http = require("http");
var options = {
  hostname: 'api.thinger.io',
  port: 80,
  path: '/v2/users/<username>/devices/<device_id>/<resource_name>',
  method: 'GET',
  headers: {
      'Authorization': 'Bearer <device_token>',
      'Accept': 'application/json, text/plain, */*'
  }
};
var req = http.request(options, function(res) {
  console.log('Status: ' + res.statusCode);
  console.log('Headers: ' + JSON.stringify(res.headers));
  res.on('data', function (body) {
    console.log('Body: ' + body);
  });
});
req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

Replace the <username>, <device_id>, <resource_name>, and <device_token> with your values (without < >).

Anyway, I will add this examples to the API explorer soon…


#5

Thank you very much, congratulations for the great work platform. :slight_smile:


#6

hi
I have used this example and replaced with my credentials but when I use the get method to read data from bucket I get problem with request: socket hang up