Hello, I am trying to Fetch a JSON from my API in Node-Red but fail to get the contents.
I have simulated my problem in the following image:
Time stamp will work locally but browsing "/test" will require a URL to get to /response
Please adjust URL at the /Get/Test side before testing
Assistance welcome.
The code follows below:
[
{
"id": "664244b99a502b3b",
"type": "tab",
"label": "Flow 1",
"disabled": false,
"info": "",
"env": []
},
{
"id": "1db96b05a975cce0",
"type": "http response",
"z": "664244b99a502b3b",
"name": "httpResponse",
"statusCode": "",
"headers": {
"Access-Control-Allow-Origin": "*"
},
"x": 720,
"y": 360,
"wires": []
},
{
"id": "dc5fed152c7ce697",
"type": "template",
"z": "664244b99a502b3b",
"name": "httpFormat",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "{\n \"credits\": \"credits\",\n \"property\":\"property\",\n \"final\":\"final\",\n \"city\":\"city\",\n \"uf\":\"uf\",\n \"man\":\"man\",\n \"model\":\"model\",\n \"year\":\"year\",\n \"cor\":\"cor\",\n \"situation\":\"situation\" \n}",
"output": "json",
"x": 530,
"y": 340,
"wires": [
[
"1db96b05a975cce0",
"7025b429af244030"
]
]
},
{
"id": "7025b429af244030",
"type": "debug",
"z": "664244b99a502b3b",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 710,
"y": 320,
"wires": []
},
{
"id": "7ea5fdfeeff75920",
"type": "node-red-contrib-http-custom-port",
"z": "664244b99a502b3b",
"name": "",
"url": "/response",
"method": "get",
"upload": false,
"swaggerDoc": "",
"port": "3100",
"x": 300,
"y": 340,
"wires": [
[
"dc5fed152c7ce697"
]
]
},
{
"id": "ef49e23bb36ad416",
"type": "node-red-contrib-http-custom-port",
"z": "664244b99a502b3b",
"name": "",
"url": "/test",
"method": "get",
"upload": false,
"swaggerDoc": "",
"port": "3000",
"x": 300,
"y": 260,
"wires": [
[
"6ff4d35fb0e3aec6"
]
]
},
{
"id": "6ff4d35fb0e3aec6",
"type": "template",
"z": "664244b99a502b3b",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template": "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">\n <title>JSON Test</title>\n</head>\n<body>\n <div id=\"myData\"></div>\n <script>\n fetch('Server URL:3100/response')\n .then(function (response) {\n return response.json();\n })\n .then(function (data) {\n appendData(data);\n })\n .catch(function (err) {\n console.log('error: ' + err);\n });\n function appendData(data) {\n var mainContainer = document.getElementById(\"myData\");\n for (var i = 0; i < data.length; i++) {\n var div = document.createElement(\"div\");\n div.innerHTML = 'Name: ' + data[i].credits + ' ' + data[i].marca;\n mainContainer.appendChild(div);\n }\n }\n </script>\n</body>\n</html>",
"output": "str",
"x": 540,
"y": 240,
"wires": [
[
"806968dacc5db796"
]
]
},
{
"id": "806968dacc5db796",
"type": "http response",
"z": "664244b99a502b3b",
"name": "httpResponse",
"statusCode": "",
"headers": {
"Access-Control-Allow-Origin": "*"
},
"x": 720,
"y": 240,
"wires": []
},
{
"id": "088ffc4886be3459",
"type": "inject",
"z": "664244b99a502b3b",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 300,
"y": 200,
"wires": [
[
"6ff4d35fb0e3aec6"
]
]
}
]