A javascript/nodejs multipart/form-data parser which operates on data from SendGrid's Inbound Parse Webhook. e.g. A SendGrid Inbound Parse webhook pointing to an Azure Function.
npm install inbound-parse-multipart-parser
...
--xYzZy
Content-Disposition: form-data; name="html"
This is the message body sent from an email.
--xYzZY
Content-Disposition: form-data; name="from"
Allistair Vilakazi
--xYzZY
Content-Disposition: form-data; name="text"
This is the message body sent from an email.
--xYzZY--
`
The lines above represents a raw multipart/form-data payload sent by the SendGrid Inbound Parse Webhook via email. We need to extract the all data contained inside it.
Usage
In the next lines you can see a implementation. In this case two key values
needs to be present:
* body, which can be:
`
--xYzZY
Content-Disposition: form-data; name="text"
This is the message body sent from an email.
--xYzZY--
`
* boundary, the string which serve as a 'separator' between parts, it normally
comes to you via headers. In this case, the boundary is:
`
xYzZY
`
Now, having this two key values then you can implement it:
`
var multipart = require('multipart.js');
var body = Buffer.from(req.rawBody),'utf-8');
var boundary = multipart.getBoundary(req.headers['content-type']);
var parts = multipart.Parse(body,boundary);
for(var i=0;i var part = parts[i];
// will be:
// { name: 'text', data: 'This is the message body sent from an email.' }
}
``