Error while copying content to a stream

Hi,

it seems I need another type of http authentification for getting JSON information from my gateway.
How I can realize such a request?

var http = require('http');
var auth = 'Basic ' + Buffer.from('username' + ':' + 'password').toString('base64');

var options = {
    hostname: '192.168.x.y',
    port: '8083',
    path: '/mypath/devices',
    method: 'GET',
    headers: {'Authorization': auth},
    json:true
}

var body='';
var callback = function(response) {
   // Continuously update stream with data
    response.on('data', function(data) {
      body += data;
   });
...
   response.on('end', function() {
      // Data received completely.
      console.log(body);
      process_data();   // here will be the main procedure
   });
}

var req = http.request(options, callback);
req.end();

I have tried a simple request only with my name/password - it is not working, that’s why I think the auth method maybe wrong.

Thanks!

That’s not how you send an HTTP request in Home Remote plugin. There are plenty examples in the forum to help guide you.

Here’s a good example you can start with. It’s very straight forward & it uses Basic Authentication.

hmm… looks like the same method as I use in my plugin. Cannot see any difference.

Will check it again, maybe it is not the authorization but other issue.

Thanks for your time.

I was talking about your entire code snippet. None of what you have is compatible. That’s not how you send a GET request with Home Remote. There’s a sample GET request in that Fibaro plugin I shared. Please review that code.

yes, sorry for confusion.
I’m testing all the code outside of Home Remote, and there it was working.
I thought it maybe an authorization problem, that’s why have included the original working code.

Today have found out that it seems to be another issue, not sure how you could reproduce it.

Hers is my (minimized) code:

plugin.Name = "ZWayTest";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 5000;
plugin.DefaultSettings = { "Host": "192.168.178.90", "Port": "8083", "Username": "", "Password": "" };

var http = new HTTPClient();
var baseURL = null;
var credentials = {}

function onChangeRequest(device, attribute, value) {
}

function onConnect() {
    baseURL = "http://" + plugin.Settings["Host"] + ":" + plugin.Settings["Port"] + "/ZAutomation/api/v1/devices";
    credentials = {
        username: plugin.Settings["Username"],
        password: plugin.Settings["Password"]
    }
}

function onDisconnect() {
}

function onPoll() {
    var r = http.get(baseURL + "/ZAutomation/api/v1/devices", { auth: credentials });
}

function onSynchronizeDevices() {
    var r = http.get(baseURL + "/ZAutomation/api/v1/devices", { auth: credentials });
    console.log(r.data);

}

I get an error “Error while copying content to a stream” what seems to be another issue - have found it here: c# - System.Net.Http.HttpRequestException Error while copying content to a stream - Stack Overflow

Using other URLs from other sources is working fine with the code.
Same request using node-js with the first code above is working also.

Have you any idea?

The Designer’s HTTPClient is requesting HTTP/2. There’s currently no way for you to force HTTP/1 like that SO solution is suggesting. I’ll try to add a way to adjust that in the next release.

ok,… understand.
When downloading a file I see in apache.log
GET /data.json HTTP/1.1" 200 53 "-" "-"
-> HTTP/1.1

if downloading from here a simple data file (txt file) all is working fine with HTTP/1.
So the HTTP/2 problem is for any special cases? I have same issue for Domoticz API calls.

And - what is also interesting for me - if I replace the line from
var r = http.get(baseURL + "/s1.txt", { auth: credentials });
to
var r = http.get(baseURL + "/data.json", { auth: credentials });

it will not work more.
content of data.json:
[{"id":1611692743,"l":3.3},{"id":1611692803,"l":3.2}]

I get an error at
console.log(r.data);

“No public methods with the specified arguments were found.”

What I must use to log the data?

Thanks!

I just uploaded Designer version 3.19.3.0. Can you please install the update & see if it fixes your issue?

1 Like

The console.log in HR is picky. Try

console.log(JSON.stringify(r.data));

@bill - THANKS - it is working now fine, for both API calls. :+1:
(made before a workaround with a php “proxy -page”, this was also working then :wink: )

@Vpow - yes, thanks, I found it out yesterday in my late evening :wink:

1 Like