无法解析多部分表单数据,因为 Busyboy 的返回数据采用某种奇怪的格式。我可以在终端中看到正确的值,但是它包含一些符号,所以不能 JSON.parse 它。
JSON.parse(fields.values)
有谁知道这是哪种格式以及如何解析它?
只是无法理解它是什么格式。
任何帮助表示赞赏。
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)
}