Host files in node red which can fetched on GET request

Hello Everyone,
I am trying to follow the example in Serve a local file : Node-RED (nodered.org) to host a local file which is present on a different folder(on a different drive) and fetch/download it if GET request is made to the node red server

The file is an binary file and content type is of application/octet-stream. All the headers are being passed
But sadly it does not seem to download or even response when i do curl on the end point(even though i see the logs)

The logic of the flow is, it would first fetch the file name in the fota.json file, then it fetch the bin file mentioned in the fota.json file and then create the respective headers, and finally serve it.

Please help if any idea.

@Shariq Welcome th the forum!

While a picture is nice, no one can see what you have actually coded. Providing the flow would be a much better idea.

Also what version of NR and you running and on what platform?

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

  1. Fetch OTA file --> ensure that you set output to signle buffer object
  2. you need to build the correct http response header which are
msg.headers = {
    "Cache-Control": "no-cache",
    "Pragma": "no-cache",
    "Content-Transfer-Encoding": "binary",
    "Content-Type": "application/octet-stream", 
    "Content-Disposition": "attachment;filename=" + msg.filename,
    "Content-Length": "" + msg.payload.length
}

your msg.payload is your file buffer you've read from Fetch OTA file node.

Hello Zenofmud,
Sorry for not posting this earlier.

Here is the json file imported from a new flow.

Also. i have added similar flow for a simple png file in the same flow but too isnt working

[
    {
        "id": "8abafcc658f0c121",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "eb32e217d0e9ae25",
        "type": "http response",
        "z": "8abafcc658f0c121",
        "name": "",
        "statusCode": "200",
        "headers": {},
        "x": 840,
        "y": 300,
        "wires": []
    },
    {
        "id": "53f429b72f9df214",
        "type": "http in",
        "z": "8abafcc658f0c121",
        "name": "",
        "url": "/lota_file",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 90,
        "y": 360,
        "wires": [
            [
                "2edb87c7d013cc50"
            ]
        ]
    },
    {
        "id": "01fe28d777300a20",
        "type": "function",
        "z": "8abafcc658f0c121",
        "name": "",
        "func": "var msg_out={};\nvar file= msg.filename;\nnode.warn(file);\nmsg_out.headers = {\n\"Cache-Control\": \"no-cache\",\n\"Pragma\": \"no-cache\",\n//\"Content-Transfer-Encoding\": \"binary\",\n\"Content-Type\": \"text/html\",\n//    \"Content-Disposition\": \"attachment;filename=\" + file,//.split(\"Home Automation\")[1],\n\"Content-Length\": \"\" + msg.payload.length\n};\nmsg_out.statusCode = 200;\nmsg_out.payload = Buffer.from(msg.payload);\nreturn msg_out;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 360,
        "wires": [
            [
                "eb32e217d0e9ae25",
                "2740573fc2492528",
                "289400e3c67f402c"
            ]
        ]
    },
    {
        "id": "2740573fc2492528",
        "type": "debug",
        "z": "8abafcc658f0c121",
        "name": "debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "headers",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 480,
        "wires": []
    },
    {
        "id": "2edb87c7d013cc50",
        "type": "file in",
        "z": "8abafcc658f0c121",
        "name": "Fetch OTA file",
        "filename": "C:\\Users\\shariq\\.node-red\\house.png",
        "filenameType": "str",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 380,
        "y": 380,
        "wires": [
            [
                "01fe28d777300a20"
            ]
        ]
    },
    {
        "id": "289400e3c67f402c",
        "type": "debug",
        "z": "8abafcc658f0c121",
        "name": "debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 380,
        "wires": []
    },
    {
        "id": "f5cbe277a9ff9bf7",
        "type": "http in",
        "z": "8abafcc658f0c121",
        "name": "",
        "url": "/ota_file",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 110,
        "y": 80,
        "wires": [
            [
                "bd812664c4ddf70f"
            ]
        ]
    },
    {
        "id": "fca5364d8e8a109a",
        "type": "function",
        "z": "8abafcc658f0c121",
        "name": "",
        "func": "var msg_out={};\nvar file= msg.file_name;\nnode.warn(file);\nmsg_out.headers = {\n\"Cache-Control\": \"no-cache\",\n\"Pragma\": \"no-cache\",\n\"Content-Transfer-Encoding\": \"binary\",\n\"Content-Type\": \"application/octet-stream\",\n\"Content-Disposition\": \"attachment;filename=\" + file,//.split(\"Home_otomation\")[1],\n\"Content-Length\": \"\" + msg.payload.length\n};\nmsg_out.statusCode = 200;\nmsg_out.payload = Buffer.from(msg.payload);\nreturn msg_out;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 660,
        "y": 80,
        "wires": [
            [
                "3da9edc8cbedb76a",
                "b41823b25c74f657",
                "bed3badedd5d4ea2"
            ]
        ]
    },
    {
        "id": "3da9edc8cbedb76a",
        "type": "debug",
        "z": "8abafcc658f0c121",
        "name": "debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "headers",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 730,
        "y": 200,
        "wires": []
    },
    {
        "id": "bd812664c4ddf70f",
        "type": "file in",
        "z": "8abafcc658f0c121",
        "name": "Read FOTA info",
        "filename": "D:\\Development\\Home_otomation\\fota.json",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 140,
        "y": 140,
        "wires": [
            [
                "b93f3b18aa9bb6e7"
            ]
        ]
    },
    {
        "id": "b93f3b18aa9bb6e7",
        "type": "json",
        "z": "8abafcc658f0c121",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": true,
        "x": 310,
        "y": 100,
        "wires": [
            [
                "abe1e9ce11532a60"
            ]
        ]
    },
    {
        "id": "f6e9e10f46f1521e",
        "type": "file in",
        "z": "8abafcc658f0c121",
        "name": "Fetch OTA file",
        "filename": "file_name",
        "filenameType": "msg",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 520,
        "y": 140,
        "wires": [
            [
                "fca5364d8e8a109a"
            ]
        ]
    },
    {
        "id": "abe1e9ce11532a60",
        "type": "function",
        "z": "8abafcc658f0c121",
        "name": "",
        "func": "var file = \"D:\\\\Development\\\\Home_otomation\";\nvar bin= msg.payload.bin+\"\";\nbin = bin.replace('/', '\\\\');\n//node.warn(\"bin \" +bin);\nmsg.file_name = file + bin;\n//node.warn(\"bin2 \" + msg.file_name);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 180,
        "wires": [
            [
                "f6e9e10f46f1521e"
            ]
        ]
    },
    {
        "id": "bed3badedd5d4ea2",
        "type": "debug",
        "z": "8abafcc658f0c121",
        "name": "debug",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 850,
        "y": 100,
        "wires": []
    },
    {
        "id": "b41823b25c74f657",
        "type": "http response",
        "z": "8abafcc658f0c121",
        "name": "",
        "statusCode": "200",
        "headers": {},
        "x": 860,
        "y": 20,
        "wires": []
    }
] 

Hello Tbdltee.

Yes i have put things exactly like that already but that still isnt working
image

var msg_out={};

var file= msg.file_name;

node.warn(file);

msg_out.headers = {

"Cache-Control": "no-cache",

"Pragma": "no-cache",

"Content-Transfer-Encoding": "binary",

"Content-Type": "application/octet-stream",

"Content-Disposition": "attachment;filename=" + file,//.split("Home_otomation")[1],

"Content-Length": "" + msg.payload.length

};

msg_out.statusCode = 200;

msg_out.payload = Buffer.from(msg.payload);

return msg_out;

the headers when printed

{
"Cache-Control":"no-cache",
"Pragma":"no-cache",
"Content-Transfer-Encoding":"binary",
"Content-Type":"application/octet-stream",
"Content-Disposition":"attachment;filename=D:\\Development\\Home_otomation\\home_automate_inc_ota_2.ino.bin",
"Content-Length":"873024"
}

You are not making sure msg.res makes it to the http response node, if you use the OG msg things should work. Also no need for the buffer.from() line.
e.g.

[{"id":"f6e9e10f46f1521e","type":"file in","z":"8abafcc658f0c121","name":"Fetch OTA file","filename":"file_name","filenameType":"msg","format":"","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":440,"y":60,"wires":[["fca5364d8e8a109a"]]},{"id":"abe1e9ce11532a60","type":"function","z":"8abafcc658f0c121","name":"","func":"var file = \"D:\\\\Development\\\\Home_otomation\";\nvar bin= msg.payload.bin+\"\";\nbin = bin.replace('/', '\\\\');\n//node.warn(\"bin \" +bin);\nmsg.file_name = file + bin;\n//node.warn(\"bin2 \" + msg.file_name);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":280,"y":60,"wires":[["f6e9e10f46f1521e"]]},{"id":"fca5364d8e8a109a","type":"function","z":"8abafcc658f0c121","name":"","func":"\nvar file= msg.file_name;\nnode.warn(file);\nmsg.headers = {\n\"Cache-Control\": \"no-cache\",\n\"Pragma\": \"no-cache\",\n\"Content-Transfer-Encoding\": \"binary\",\n\"Content-Type\": \"application/octet-stream\",\n\"Content-Disposition\": \"attachment;filename=\" + file,//.split(\"Home_otomation\")[1],\n\"Content-Length\": \"\" + msg.payload.length\n};\nmsg.statusCode = 200;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":60,"wires":[["3da9edc8cbedb76a","b41823b25c74f657","bed3badedd5d4ea2"]]},{"id":"f5cbe277a9ff9bf7","type":"http in","z":"8abafcc658f0c121","name":"","url":"/ota_file","method":"get","upload":false,"swaggerDoc":"","x":130,"y":60,"wires":[["abe1e9ce11532a60"]]},{"id":"3da9edc8cbedb76a","type":"debug","z":"8abafcc658f0c121","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"headers","targetType":"msg","statusVal":"","statusType":"auto","x":730,"y":200,"wires":[]},{"id":"b41823b25c74f657","type":"http response","z":"8abafcc658f0c121","name":"","statusCode":"","headers":{},"x":770,"y":60,"wires":[]},{"id":"bed3badedd5d4ea2","type":"debug","z":"8abafcc658f0c121","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":100,"wires":[]}]

Omg, such a subtle but silly mistake i did

Thank you very much :slight_smile:

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