0

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

libzz
  • 589
  • 2
  • 10
  • 29

1 Answers1

1

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.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • I can't do this because the request is going to be forwarded to a server which expects a multipart/form-data content-type. I need to extract part of this data to be used some place in the flow before sending it off to the server. – libzz May 17 '19 at 07:05
  • Then you should have made that clear in the original question. But it doesn't matter as this is the only option available to you. If you need to send the request on to another server, there is all the information you need to rebuild the request before sending it on. – hardillb May 17 '19 at 07:41
  • On another perspective, if I use your suggestion, I could use "Accept File uploads" and receive the data in `msg.req.files` then recreate the `multipart/form-data` request from within Node-RED. Is that possible? I have researched this workaround before but didn't find any reference. – libzz May 20 '19 at 00:05
  • It seems to be possible to manually create a multipart/form-data in a function node so I'm accepting this answer. Reference: https://stackoverflow.com/questions/55698724/how-to-create-multipart-http-request-in-node-red/56319183#56319183 – libzz May 27 '19 at 07:01