admin管理员组

文章数量:1277269

I want to extract the binary content from a multipart response that I have as follows -

I do not have access to any request object (Express/NodeJS). I just have this response object which I want to parse and extract the binary part of it.

EDIT -

To make things more clear -

  1. I have an endpoint (Not a traditional Express app but FaaS) - called "/fileUpload"

  2. This endpoint accepts all kind of requests - GET, POST, PUT (FaaS).

  3. When I make a POST/PUT request with a file - testImage.PNG in the form data, I receive the content in the request object. FaaS runtime provides the req object as a function handler's parameter.

  4. Now, when I print - request.body of the ining POST request, I get the content as show in the image above.

  5. If I try to upload this binary content directly to S3, it leads to corruption of the image which I believe is due to the presence of additional content in the multipart response like - Content-Disposition...

What I would like to do is, parse the additional multipart content like the Content-Type header and multipart boundary etc, so that I get only the image binary.

I want to extract the binary content from a multipart response that I have as follows -

I do not have access to any request object (Express/NodeJS). I just have this response object which I want to parse and extract the binary part of it.

EDIT -

To make things more clear -

  1. I have an endpoint (Not a traditional Express app but FaaS) - called "/fileUpload"

  2. This endpoint accepts all kind of requests - GET, POST, PUT (FaaS).

  3. When I make a POST/PUT request with a file - testImage.PNG in the form data, I receive the content in the request object. FaaS runtime provides the req object as a function handler's parameter.

  4. Now, when I print - request.body of the ining POST request, I get the content as show in the image above.

  5. If I try to upload this binary content directly to S3, it leads to corruption of the image which I believe is due to the presence of additional content in the multipart response like - Content-Disposition...

What I would like to do is, parse the additional multipart content like the Content-Type header and multipart boundary etc, so that I get only the image binary.

Share Improve this question edited Sep 17, 2019 at 13:54 Boudhayan Dev asked Sep 17, 2019 at 11:18 Boudhayan DevBoudhayan Dev 1,0296 gold badges22 silver badges43 bronze badges 1
  • This looks like a file upload of a file named testImage.PNG. Do you want to store the file in your server's filesystem? Place it in an array? Or what? Please edit your question to clarify your requirement. – O. Jones Commented Sep 17, 2019 at 12:05
Add a ment  | 

3 Answers 3

Reset to default 3

To extract the binary content, we need to remove/parse the header and boundary sections of it. There exists a library - parse-multipart which makes it easy to do so.

Example -

Assuming request header contains a content-type header.

var multipart = require('parse-multipart');
// The following section is the express endpoint/ FaaS handler
    {

        let header = req.headers["content-type"]
        let boundary  = header.split(" ")[1]
        boundary = header.split("=")[1]
        let body = event.data;
        //console.log("Boundary - "+ boundary)
        let parts = multipart.Parse(body, boundary);
        for (var i = 0; i < parts.length; i++) {
            var part = parts[i];
            console.log(part)

            var params = {
                Bucket: s3bucket,
                Key: part.file,
                Body: part.data,
                ACL: 'public-read',
                Metadata: {
                    'Content-Type': part.type
                }
            };
            //Upload the data to aws
            const data = await s3.putObject(params).promise()


        }
        return parts

    }

I struggled for many days with this problem. The solution was in the API GATE WAY > configuration > Binary types add multipart/form-data.

Observation on parse-multipart library: Be warned, this does not provide the 'name' directive of the multi-part body.

The documentation is very sketchy, reporting of error, really is not there. Apparently valid multi-part body is not parsed and the reason would not be made clear by the library.

For more pliant processing, the libraries listed in the following URL could be considered:

https://www.npmjs./package/body-parser

本文标签: javascriptHow to extractparse the binary content from a Multipart response in NodeJSStack Overflow