Basic Fetch Error

Another way to approach this, hope this helps:

<?php
$api = 'http://41.192.149.206:8080/query?select=[time.iso,ch1.d2,ch1.amps.d2,ch1.va.d2,ch1.pf.d2]&begin=h-30m&end=h&group=30m&format=json&header=yes';
$out = file_get_contents($api);
echo $out;

Then:

php nr-test.php

Yielding this mess (the problem):

0086
{"range":[1583087400,1583089200],"labels":["Time","ch1","ch1","ch1","ch1"],
"data":[["2020-03-01T20:30:00",531.63,2.32,542.09,0.98]]}
0000

Clearly showing the problem (above).

Then (just a quick kludge), do this:

<?php
$api = 'http://41.192.149.206:8080/query?select=[time.iso,ch1.d2,ch1.amps.d2,ch1.va.d2,ch1.pf.d2]&begin=h-30m&end=h&group=30m&format=json&header=yes';
//$out = file_get_contents($api);
$stuff = file($api);
$out = $stuff[1].$stuff[2];
echo $out;

Which I did (to give you some ideas), and moved to the web and put this test endpoint in an HTTP REQUEST node, like this:

Giving the desired result.

A total kludge.. LOL. But it helps reveal the problem and potential solutions and work arounds :slight_smile:

In other words, one approach is to use an intermediary process to "clean up the mess" before processing.

1 Like

Yes, that is how you export a flow. However you have exported the flow with your HTTP Request node in. I'd like to see the one with your Exec node in.

In such case my guess is that the HTTP Toolkit is interfering in the results. It is not a surprise as it sits in the middle of the conversation. Somehow the tool is able to retrieve and forward to Node-RED part of the HTTP response. This tool is for troubleshooting only and can not be used in any other way.

1 Like

Above my pay-grade LOL. Kludge or not - it works :wink:
I will need to figure out how i could use that in reality.. TX Neo

You are welcome.

Sometimes you need to get "out of the box" to see what is in the box.

In this case, the PHP API call reveals the malformed API response; and with such a malformed response, most JS JSON solutions "in the box" will fail (as you are seeing).

This is not a Node-RED issue, per se. The issue is a malformed API response, so all I did was create an intermediary process to clean things up.

In Node-RED you could change this around a bit, and form the query and pass it to the HTTP REQUEST node which would call the "intermediate" API which cleans up "the mess" as I did in my example I tossed together for you.

It's actually quite simple (out of the box). Sometimes it's fun to improvise because it helps us see other solutions as well. Hopefully, this gives you some ideas.

:slight_smile:

To be honest, adding yet another dependency on an external webserver seems like one kludge too far to me.

Here's another approach that is self-contained in Node-RED: use the TCP Request node in Node-RED and do the HTTP request from scratch... I've even done the parsing of the response for you...

[{"id":"b02df2ce.532e1","type":"inject","z":"12e1febf.0d0201","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":720,"wires":[["623db68f.934af8"]]},{"id":"623db68f.934af8","type":"template","z":"12e1febf.0d0201","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"GET /query?select=%5Btime.iso,ch1.d2,ch1.amps.d2,ch1.va.d2,ch1.pf.d2%5D&begin=h-30m&end=h&group=30m&format=json&header=yes HTTP/1.1\nHost: 41.192.149.206:8080\nUser-Agent: curl/7.65.1\nAccept: */*\n\n","output":"str","x":280,"y":720,"wires":[["8e60424b.b40c8"]]},{"id":"8e60424b.b40c8","type":"tcp request","z":"12e1febf.0d0201","server":"41.192.149.206","port":"8080","out":"time","splitc":"2000","name":"","x":490,"y":720,"wires":[["408c796c.ded338","7ff73210.e471ec"]]},{"id":"408c796c.ded338","type":"function","z":"12e1febf.0d0201","name":"parse response","func":"let parts = msg.payload.toString().split(/\\r\\n\\r\\n/);\n\n// Drop the headers\nparts.shift();\n\nparts.forEach(p => {\n    let lines = p.split(\"\\n\");\n    if (lines[0] === '') {\n        return;\n    }\n    let length = parseInt(lines[0],16);\n    if (length === 0) {\n        return;\n    }\n    let text = \"\";\n    for (let i=1;i<lines.length;i++) {\n        text+=lines[i]+\"\\n\";\n    }\n    text = text.substring(0,length);\n    msg.payload = text;\n    \n})\n\ntry {\n    msg.payload = JSON.parse(msg.payload);\n} catch(err) {\n    node.error(err,msg);\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":780,"wires":[["32b925c2.1f57fa"]]},{"id":"7ff73210.e471ec","type":"debug","z":"12e1febf.0d0201","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":720,"wires":[]},{"id":"32b925c2.1f57fa","type":"debug","z":"12e1febf.0d0201","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":780,"wires":[]}]
5 Likes

I agree @knolleary 1000%

But the kludge reveals the problem clearly and then it's easy to create even better solutions.

Generally, when we know what the exact problem is, the solutions are easy (always).

Great job with Node-RED.

Truly an amazing "work or art" by you and your team.

3 Likes

I am very grateful. A lot more in depth than i expected :wink:
Thank You Nick!

OBTW,

@knolleary

I loaded your TCP Request node solution and learned a lot from you in that bit of code (for Node-RED), so thank you for taking the time to teach @Waynedejager and "show us all the way".

Bravo!

What is this data coming from?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.