Basic Fetch Error

Trying to do basic fetch

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?

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.

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

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

1 Like

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

The output ( if you care to check ) is something like this :-1:

{
    "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" 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)

Stuck...^%#^%$

@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:

  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?

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?

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

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?

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

Can you share how you have configured the exec node?

@Colin it doesn't

I did an HTTP get request using the HTTP toolkit, 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).

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

1 Like

Voila - some action :wink: 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.

HTTP Toolkit is an app, not a node.

HTTP Toolkit is a beautiful & open-source 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

1 Like

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.

[{"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.

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

@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?

TX @unixneo

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.

Like This Nick?

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

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