Friday, 26 May 2017

Match key and value of splitted Boundary

request.on('end', function () {
   if (request.headers.hasOwnProperty('content-type') && request.headers['content-type'].indexOf('boundary=') > -1) {
       var parts = request.headers['content-type'].split('boundary=');
       var boundary = parts[1];
       var splitBody = requestBody.split(boundary);

       console.log(splitBody);
       res.writeHead(200, { "Content-Type": "application/json" });
       res.write(JSON.stringify({ formFields: splitBody }));
   }
   else {
       //bad request
       res.writeHead(400, { "Content-Type": "text/paint" });
       res.write("missing boundary in content-type ");
       }
});

So I get this (console.log):

[ '--',
'\r\nContent-Disposition: form-data; name="image"\r\n\r\n/ap.jpg\r\n--',
'\r\nContent-Disposition: form-data; name="name"\r\n\r\nsmth\r\n--',
'--\r\n' ]

So basically what I want to do is iterate over splitBody and match the key and value and add it to array of fields. What is the best way to do that?



via igodie

No comments:

Post a Comment