Data Query NODE JS

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

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

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

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…

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

1 Like

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