How to send "put /items/{itemname}/state" from node.js to oh2?

Hello to all,
I’am trying to connect a modbus master to oh2.
I started with node.js because it was the easiest way to get a running Modbus TCP -Slave.

I get now an event every time, when the modbus master changes a value. I would like to send the value then to oh2.
But I do not understand at all how to create post/put message from node.js to be oh2 conform.

Can somebody help me with tips.
I tried with “Request - Simplified HTTP client” npm-Package. But I have no clue what to do.

1 Like

Hello,
I changed my strategie and changed over to node.js “node-rest-client”. I think that one would do.

I changed the example code, and I get an error reesponse (but at least I get a response :slight_smile: )
Here comes the code. I think I have problems with the data block. What do I have to change?

//Example POST method invocation
var Client = require('node-rest-client').Client;

var client = new Client();

// set content-type header and data as json in args parameter
var args = {
    data: "123",
    headers: { "Content-Type": "application/json" }
};

client.put("http://hab:8080/rest/items/HzgAussenTempMB/state", args, function (data, response) {
    // parsed response body as js object
    console.log(data);
    // raw response
    console.log(response);
});

The answer to that call is:

{ error:
   { message: 'HTTP 415 Unsupported Media Type',
     'http-code': 415,
     exception:
      { class: 'javax.ws.rs.NotSupportedException',
        message: 'HTTP 415 Unsupported Media Type',
        'localized-message': 'HTTP 415 Unsupported Media Type' } } }
IncomingMessage {
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: true,
     ended: true,
     endEmitted: true,
     reading: false,
     sync: true,
     needReadable: false,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: false,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: false,
...........

I found the error in

I changed to

var args = {
    headers: { "Content-Type": "text/plain",
               "Accept": "application/json"},
    data: "123"
};

and the value becomes changed. What a race!