0

Unable to parse multipart form data as the return data from Busyboy is in some weird format. I can see the correct values in the terminal however it contains some symbols so couldn't JSON.parse it.

JSON.parse(fields.values)

Does anyone know which format this is and how to parse it?

Just couldn't understand what format it is.

Any help appreciated.

weird format

function parseMultipartForm(event) {
      return new Promise((resolve) => {
        // we'll store all form fields inside of this
        const fields = {};
    
        // let's instantiate our busboy instance!
        const busboy = new Busboy({
          // it uses request headers
          // to extract the form boundary value (the ----WebKitFormBoundary thing)
          headers: event.headers
        });
    
        // before parsing anything, we need to set up some handlers.
        // whenever busboy comes across a file ...
        busboy.on(
          "file",
          (fieldname, filestream, filename, transferEncoding, mimeType) => {
            // ... we take a look at the file's data ...
            filestream.on("data", (data) => {
              // ... and write the file's name, type and content into `fields`.
              fields\[fieldname\] = {
                filename,
                type: mimeType,
                content: data,
              };
            });
          }
        );
    
        // whenever busboy comes across a normal field ...
        busboy.on("field", (fieldName, value) => {
          // ... we write its value into `fields`.
          fields\[fieldName\] = value;
        });
    
        // once busboy is finished, we resolve the promise with the resulted fields.
        busboy.on("finish", () => {
          resolve(fields)
        });
    
        // now that all handlers are set up, we can finally start processing our request!
        busboy.write(event.body);
      });
    }
    
    exports.handler = async (event, context) => {
    
      const fields = await parseMultipartForm(event)
    
      console.log(fields)
    
    }
Harsha Murupudi
  • 579
  • 1
  • 6
  • 19

0 Answers0