# Basic Fetch Error

**URL:** <https://discourse.nodered.org/t/basic-fetch-error/22416>\
**Category:** General\
**Created:** [29 February 2020 17:17 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416 "2020-02-29T17:17:17Z")\
**Posts on this page:** 11\
**Page:** 2

<div class="post-metadata">

**Author:** ![unixneo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unixneo/32/17441_2.png) [@unixneo](https://discourse.nodered.org/u/unixneo)\
**Post date:** [1 March 2020 19:42 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/21 "2020-03-01T19:42:31Z")

</div>

Another way to approach this, hope this helps:

```auto
<?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):

```auto
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:

```auto
<?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:

 ![Screen Shot 2020-03-02 at 2.42.15 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1cc610139bc9d90a38f7fa923917bc0a3bc42a53.jpeg)

Giving the desired result.

 ![Screen Shot 2020-03-02 at 2.44.40 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ed4f5e5ccae499bbef9c0782a9726b7f3763043.jpeg)

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

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 March 2020 19:46 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/22 "2020-03-01T19:46:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [1 March 2020 19:50 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/23 "2020-03-01T19:50:36Z")

</div>

> [@Waynedejager](#):
>
> @Andrei I test with your imported code and I get "HPE INVALID CONSTANT" error...
> 
> Am i using old software or something ? How can it work on your Node-Red and not mine ? any Ideas?

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.

---

<div class="post-metadata">

**Author:** ![Waynedejager](https://avatars.discourse-cdn.com/v4/letter/w/bc79bd/32.png) [@Waynedejager](https://discourse.nodered.org/u/Waynedejager)\
**Post date:** [1 March 2020 19:50 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/24 "2020-03-01T19:50:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unixneo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unixneo/32/17441_2.png) [@unixneo](https://discourse.nodered.org/u/unixneo)\
**Post date:** [1 March 2020 19:57 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/25 "2020-03-01T19:57:46Z")

</div>

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.

🙂

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 March 2020 20:16 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/26 "2020-03-01T20:16:56Z")

</div>

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...

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![unixneo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unixneo/32/17441_2.png) [@unixneo](https://discourse.nodered.org/u/unixneo)\
**Post date:** [1 March 2020 20:21 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/27 "2020-03-01T20:21:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Waynedejager](https://avatars.discourse-cdn.com/v4/letter/w/bc79bd/32.png) [@Waynedejager](https://discourse.nodered.org/u/Waynedejager)\
**Post date:** [1 March 2020 20:25 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/28 "2020-03-01T20:25:03Z")

</div>

> [@knolleary](#):
>
> I've even done the parsing of the response for you...

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

---

<div class="post-metadata">

**Author:** ![unixneo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/unixneo/32/17441_2.png) [@unixneo](https://discourse.nodered.org/u/unixneo)\
**Post date:** [1 March 2020 20:28 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/29 "2020-03-01T20:28:34Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 March 2020 21:44 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/30 "2020-03-01T21:44:18Z")

</div>

What is this data coming from?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [15 March 2020 21:44 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/31 "2020-03-15T21:44:29Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/basic-fetch-error/22416.md?page=1)
