Search code examples
multipartform-datanode-red

How to extract/parse data from a multipart/form-data request in Node-RED?


I have an HTTP-in node that receives a request containing the following payload:

------WebKitFormBoundaryk5AazdSJAKEDRWS9
Content-Disposition: form-data; name="file"; filename="Sample1.csv"
Content-Type: application/vnd.ms-excel

< ... csv data here ... >

------WebKitFormBoundaryk5AazdSJAKEDRWS9
Content-Disposition: form-data; name="data"; filename="Sample1.json"
Content-Type: application/json

< ... json data here ... >

------WebKitFormBoundaryk5AazdSJAKEDRWS9--

How do I extract/parse the data or file content?

Sadly, I am unable to add libraries like formidable to Node-RED due to lack of access rights.


Update:
It seems to be possible to manually create a multipart/form-data in a function node. So, I ended up using sir @hardillb's answer and recreated the multipart/form-data request in the flow.

Reference: How to create multipart HTTP request in Node-RED


Solution

  • You shouldn't need to to use anything else. Just make sure the "Accept File uploads" box is ticked and the files should be available under msg.req.files

    With the following structure:

    [
      {
        fieldname: "file",
        originalName: "Sample1.csv",
        encoding: "7bit",
        mimeType: "application/vnd.ms-excel",
        buffer: [...],
        size: 2345
      },
      {
        fieldname: "data",
        originalName: "Sample1.json",
        encoding: "7bit",
        ...
      }
    ]
    

    The msg.req.files[0].buffer contains the content of the first file.