# 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:** 20\
**Page:** 1

<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:** [29 February 2020 17:17 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/1 "2020-02-29T17:17:18Z")

</div>

Trying to do basic fetch

```auto
fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    console.log(data);
  });

```

Debug Error function : (error)

"ReferenceError: fetch is not defined (line 1, col 1)"

Any workaround with Fetch?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 February 2020 18:05 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/2 "2020-02-29T18:05:57Z")

</div>

Are you trying to do this in a function node?

`fetch` is not native JavaScript so you need a library or module to help.

But with Node-RED, you can use "nodes" instead. In this case, you can use the http-request node.

That node will return both the content of the request plus the headers and some other data should you need it.

---

<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 14:20 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/3 "2020-03-01T14:20:08Z")

</div>

What module or library could you suggest?  
I have already tried http get node but get hpe constant errors ( payload Parse error )

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [1 March 2020 14:48 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/4 "2020-03-01T14:48:50Z")

</div>

Hi Wayne,

It will depend on where you are executing this code. If you're in a function node, it won't work. If you're executing it in a client side browser script through, for example, the `node-red-dashboard`, it should work.  
This is because the function node works on server-side javascript, otherwise known as NodeJS, while the client side code is browser javascript.

The Fetch API that you are using, is a Web API on the _browser_ side: [https://fetch.spec.whatwg.org/](https://fetch.spec.whatwg.org/)  
If you're in a function node, you have to use Node APIs. However, as Julian describes above, it may help describing what you're trying to do, as for http requests it's usually easier to set it up through the core http-request node. Take a look at the following examples from the Node-RED cookbook: [https://cookbook.nodered.org/#http-requests](https://cookbook.nodered.org/#http-requests)

---

<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 15:46 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/5 "2020-03-01T15:46:08Z")

</div>

I am feeling so confused right now... What i need to to seems so simple but i am not getting it right.

I have a device which outputs values in JSON when queried via URL :

[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](http://41.192.149.206:8080/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)

The output ( if you care to check ) is something like this 👎

```auto
{
    "range": [
        1583073000,
        1583074800
    ],
    "labels": [
        "Time",
        "ch1",
        "ch1",
        "ch1",
        "ch1"
    ],
    "data": [
        [
            "2020-03-01T16:30:00",
            1475.99,
            6.41,
            1485.15,
            0.99
        ]
    ]
}

```

The "Range" output is not important to me , what i need to capture is the "labels" & corresponding "data". Its 5 labels and 5 sets of Data...

I have tried the HTTP request node using "get URL" - it does not work for me - it produces an HPE error ( i think because the output is Json and its expecting html ? ( not sure but regardless it doesnt work ).

The link you sent me has 1 particular reference to this page "[https://cookbook.nodered.org/http/set-query-string](https://cookbook.nodered.org/http/set-query-string)" which i think is what i am after , but i cannot seem to understand the correct Nodes to use, for example in the "inject node" the article seems to be using "Query Parameter" but i dont have this in my inject node ? (see screenshot)

 ![Screenshot 2020-03-01 at 17.44.28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/2/6229642ae7f119148d0f9fb242da1291f1b2c055.png)

Stuck...^%#^%$

---

<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 16:29 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/6 "2020-03-01T16:29:39Z")

</div>

@Waynedejager looking at the URL you've provided, I can see its using chunked transfer-encoding and I _think_ its slightly malformed in its response.

When I get it with `curl -vvv` I see the error:

> - Leftovers after chunking: 5 bytes

This tells us the response has 5 bytes more in it than expected (I think). It would appear that curl and browsers know how to handle this sort of error by assuming those extra bytes are meant to be there. It appears the node.js HTTP library is less forgiving.

I just tried this code in a node.js shell using the core HTTP library:

```auto
  var http = require("http");
  var req = http.get({
     hostname: "41.192.149.206",
     port: 8080,
     path: '/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'
  },(res) => { }).on('error', (e) => {
  console.error(e);
})

```

So I don't think you'd have much luck with other http modules that use this library under the covers.

Is there any scope to modify the server implementation?

---

<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 16:36 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/7 "2020-03-01T16:36:01Z")

</div>

> [@knolleary](#):
>
> Is there any scope to modify the server implementation?

Unfortunately Not. Is it possible to use curl in Node-Red? I dont really care for how we get the data in - as long as it comes in... ( This could make sense as to why i was being told by others to use a fetch javascript command ? maybe also less forgiving than HTTP GET ) Tx for assistance thus far @knolleary - can we curl?

---

<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 16:37 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/8 "2020-03-01T16:37:12Z")

</div>

Yes, you could use the exec node to run the curl command

---

<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 17:06 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/9 "2020-03-01T17:06:05Z")

</div>

I tried ... curl seems to serve up the html page prior to the query ( i.e its not giving the json output like a browser would ) - any ideas?

---

<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 17:12 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/10 "2020-03-01T17:12:14Z")

</div>

Does the page run javascript in the browser to get the data? If so then your task is tricky.

---

<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 17:14 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/11 "2020-03-01T17:14:07Z")

</div>

Can you share how you have configured the exec node?

@Colin it doesn't

---

<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 17:18 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/12 "2020-03-01T17:18:39Z")

</div>

![Screenshot 2020-03-01 at 19.18.14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c9367a60cd923f5070cf94d3949f0ba162e3c99a.png)

---

<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 18:12 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/13 "2020-03-01T18:12:12Z")

</div>

I did an HTTP get request using the [HTTP toolkit](https://httptoolkit.tech/), with some weird results.

I got no error in Node-RED and below result is displayed with a debug node. Looks like this is not the format expected (as per the OP).

 ![http-weird-response](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/ee128736e95be5e35b4d1f504fef8600cac8325b.png)

On the other hand HTTP toolkit report some issue.: "The request was aborted before the response was completed."

 ![http-weird-response-2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/512598b9ddd9fd516659be4b6fe6561a2c75d74b.png)

---

<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 18:18 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/14 "2020-03-01T18:18:51Z")

</div>

Voila - some action 😉 What node did you use in Node-Red ? IS HTTPToolkit a Node i can add from Palette? If not, please let me know how you did it in Node-Red - i will be grateful.

---

<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 18:32 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/15 "2020-03-01T18:32:18Z")

</div>

HTTP Toolkit is an app, not a node.

> HTTP Toolkit is a beautiful & [open-source](https://github.com/httptoolkit) tool  
> for debugging, testing and building with HTTP(S)  
> on Windows, Linux & Mac.

The details are in the link kindly provided in the earlier post by @andrei

---

<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 18:35 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/16 "2020-03-01T18:35:51Z")

</div>

> [@Waynedejager](#):
>
> Voila - some action 😉 What node did you use in Node-Red ? IS HTTPToolkit a Node i can add from Palette? If not, please let me know how you did it in Node-Red - i will be grateful.

I am afraid that the outcome of my testing is bad news actually. There is something that is not consistent. I need to spare time to redo the testing and report the results.

I tested using the most basic flow possible. Just an http request node. See below.

```auto
[{"id":"a4f5363d.1c4968","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"b30a55f6.9598f8","type":"debug","z":"a4f5363d.1c4968","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":180,"wires":[]},{"id":"41cc11c9.49e21","type":"inject","z":"a4f5363d.1c4968","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":180,"wires":[["c9495473.5ba4f8"]]},{"id":"c9495473.5ba4f8","type":"http request","z":"a4f5363d.1c4968","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"http://41.192.149.206:8080/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'","tls":"","persist":false,"proxy":"","authType":"","x":430,"y":180,"wires":[["b30a55f6.9598f8"]]}]

```

HTTP Toolkit is an external tool to intercept HTTP requests and responses. It is not related to Node-RED.

I tested in my Windows machine. I wish I could test on a Raspberry PI to compare the results, but not possible for some time.

```auto
1 Mar 15:21:04 - [info] Node-RED version: v1.0.3
1 Mar 15:21:04 - [info] Node.js version: v8.11.1
1 Mar 15:21:04 - [info] Windows_NT 10.0.17763 x64 LE

```

---

<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 18:42 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/17 "2020-03-01T18:42:31Z")

</div>

@Andrei I test with your imported code and I get "HPE INVALID CONSTANT" error...

 ![Screenshot 2020-03-01 at 20.40.04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df84de25a9cbc343ad574379a1823925d212b7bc.png)

Am i using old software or something ? How can it work on your Node-Red and not mine ? any Ideas?

---

<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 18:43 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/18 "2020-03-01T18:43:04Z")

</div>

TX @unixneo

---

<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 18:50 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/19 "2020-03-01T18:50:40Z")

</div>

Rather than a screenshot of your exec node, can you export it and share it's JSON here? Select the node, press Ctrl-E and copy the JSON out of the dialog that is shown. Then paste it here and click the 'format code `</>`' icon in the toolbar.

---

<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 18:54 UTC](https://discourse.nodered.org/t/basic-fetch-error/22416/20 "2020-03-01T18:54:12Z")

</div>

Like This Nick?

```auto
[{"id":"1ed0b02c.9a74e","type":"debug","z":"95ee6131.4f4b4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":180,"wires":[]},{"id":"b030dc76.6c61","type":"inject","z":"95ee6131.4f4b4","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":180,"wires":[["c29e4ad9.3a4568"]]},{"id":"c29e4ad9.3a4568","type":"http request","z":"95ee6131.4f4b4","name":"","method":"GET","ret":"obj","paytoqs":false,"url":"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=csv&header=yes","tls":"","persist":false,"proxy":"","authType":"","x":430,"y":180,"wires":[["1ed0b02c.9a74e"]]}]

```

```auto
1 Mar 20:52:30 - [info] Node-RED version: v1.0.3
1 Mar 20:52:30 - [info] Node.js version: v12.16.1
1 Mar 20:52:30 - [info] Darwin 18.7.0 x64 LE

```

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