Req.body is not giving output as expected of formData for NextApiRequest

Viewed 117

This is the front-end logic , data is react-hook-form onSubmit data. draftURL is type FileList. Request is successful but req.body is of type string.

const uploadData = new FormData();
        uploadData.append("draft", data.draftURL[0], data.draftURL[0].name);
        const response = await axios.post("/api/fileUpload", uploadData, {
            headers: {
                accept: "application/json",
                "Content-Type": `multipart/form-data`,
            },
        });

I saw multiple solutions using next-connect and multer , but they did not work very well for me. And I am also confused about why is this not working.

async function handler(req: NextApiRequest, res: NextApiResponse) {
    console.log(typeof req.body);
    const params = {
        Bucket: process.env.AWS_BUCKET,
        Key: "cat.pdf", // File name you want to save as in S3
        Body: req.body,
        conditions: [{ acl: "public-read" }],
    };

    // Uploading files to the bucket
    s3.upload(params, function (err, data) {
        if (err) {
            throw err;
        }
        console.log(`File uploaded successfully. ${data.Location}`);
        res.json(data.Location);
    });
}
0 Answers
Related