ioBroker / ioBroker.javascript

Script engine for JavaScript and Blockly
MIT License
322 stars 120 forks source link

[Bug]: second parameter of httpPost() - Usage of JSON (conforming to documentation) leads to strange behaviour #1633

Open MartinP1 opened 1 week ago

MartinP1 commented 1 week ago

I'm sure that

Script type

JavaScript

The problem

Behavior: It seems, that the examples concerning the httpPost() call are misleading. In my example, queries for lua pages of fritzbox are issued. I was not able to construct a JSON-pattern, that is working. A raw passing of body text was successful.

I think it would be helpful, either to correct the documentation, or to change the behaviour of the API-call to be conform with the documentation.

httpPost replacement of the following code:

    var options = {
        url: docsisURL,
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        body: 'xhr=1&sid=' + sid + '&lang=de&page=docInfo&xhrId=all&no_sidrenew='
    };
    request(options, function(error, response, body) {
....

this code works (adoptation of url to an "Echo Server" for http-posts)


httpPost('https://httpbin.org/post',
      'xhr=1&sid=12345678&lang=de&page=docInfo&xhrId=all&no_sidrenew=', 
        {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    },
        (error, response) => {
.....

this does not:

httpPost('https://httpbin.org/post',
       {
           form : {
               'lang' : 'de',
               "no_sidrenew" : "",
               "page" : "docInfo",
               "sid" : "12345678",
               "xhr" : "1",
               "xhrId" : "all",
           }

       },
        {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
    },
        (error, response) => {
....

It seems, that there is an internal "Jsonification" of second parameter

Response of echo server to first variant (working):

script.js.Spielwiese.Ressourcentest: data: { "args": {}, "data": "", "files": {}, "form": { "lang": "de", "no_sidrenew": "", "page": "docInfo", "sid": "12345678", "xhr": "1", "xhrId": "all" }, "headers": { "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, compress, deflate, br", "Baggage": "sentry-environment=production,sentry-release=iobroker.javascript%408.3.1,sentry-public_key=f3b9740caaee4ee69eb68019d71526ff,sentry-trace_id=4e9d2599655c4da3ba2864550274d0a8", "Content-Length": "62", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "Sentry-Trace": "4e9d2599655c4da3ba2864550274d0a8-83cbd8953e002594", "User-Agent": "Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/121.0", "X-Amzn-Trace-Id": "Root=1-6689283a-429cceaf45d9ae7e459c9e1d" }, "json": null, "origin": "1xxxxxxxxxxxx3", "url": "https://httpbin.org/post" } 

Response of echo server to second variant (not working with fritzbox)

script.js.Spielwiese.Ressourcentest: data: { "args": {}, "data": "", "files": {}, "form": { "form[lang]": "de", "form[no_sidrenew]": "", "form[page]": "docInfo", "form[sid]": "12345678", "form[xhrId]": "all", "form[xhr]": "1" }, "headers": { "Accept": "application/json, text/plain, */*", "Accept-Encoding": "gzip, compress, deflate, br", "Baggage": "sentry-environment=production,sentry-release=iobroker.javascript%408.3.1,sentry-public_key=f3b9740caaee4ee69eb68019d71526ff,sentry-trace_id=4e9d2599655c4da3ba2864550274d0a8", "Content-Length": "122", "Content-Type": "application/x-www-form-urlencoded", "Host": "httpbin.org", "Sentry-Trace": "4e9d2599655c4da3ba2864550274d0a8-9d6cc91120f1be44", "User-Agent": "Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/121.0", "X-Amzn-Trace-Id": "Root=1-668929cd-775f6c7671de680b3ea28670" }, "json": null, "origin": "17xxxxxxxxxxxx43", "url": "https://httpbin.org/post" } 

iobroker.current.log (in debug mode!)

No response

Version of nodejs

20.15.0

Version of ioBroker js-controller

5.0.19

Version of adapter

v8.3.1 and 8.6.0

MartinP1 commented 1 week ago

I have not tested the behaviour with current beta version! Not sure whether present in beta too. Will remove the check in the issue text above.

EDIT: Now Tested with 8.6.0 - same behaviour...

mcm1957 commented 1 week ago

Please Test with current beta. Thats the pirpose of the check.

Otherwise the issue might be closed.

MartinP1 commented 1 week ago

I have only one instance of ioBroker (the "productive" one), and do not want to bring instability to my system by beta adapters.

Is it possible to install a second "beta" instance of javascript adapter to my system?

That is the only thing I am able to offer ...

winnyschuster commented 1 week ago

this issue also exists in newest beta, per the doku 2nd param could be an object, but this is not working, only strings lead to a succesful result