NextJS Handling binary data

hey, how do you change the body parser in nextjs? I am trying to handle raw binary file POST requests
6 Replies
WOLFLEADER
WOLFLEADEROP3y ago
I am currently doing
const handler = async (req: ImgRequest, res: NextApiResponse) => {
res.status(200).send("OK");

const buff = Buffer.from(req.body);
console.log(req.headers);

fs.writeFileSync("G:/dump/image.jpg", buff);
};

export default handler;

export const config = {
api: {
bodyParser: {
type: "image/jpeg",
limit: "1mb",
},
},
};
const handler = async (req: ImgRequest, res: NextApiResponse) => {
res.status(200).send("OK");

const buff = Buffer.from(req.body);
console.log(req.headers);

fs.writeFileSync("G:/dump/image.jpg", buff);
};

export default handler;

export const config = {
api: {
bodyParser: {
type: "image/jpeg",
limit: "1mb",
},
},
};
however the image fule that gets saved is corrupt
WOLFLEADER
WOLFLEADEROP3y ago
seems like its saving it incorrectl
WOLFLEADER
WOLFLEADEROP3y ago
what it should look like
WOLFLEADER
WOLFLEADEROP3y ago
what im getting
tom0619
tom06193y ago
I think you can just set the bodyParse to false
export const config = {
api: {
bodyParser: false,
},
}
export const config = {
api: {
bodyParser: false,
},
}
ref: https://nextjs.org/docs/api-routes/request-helpers
WOLFLEADER
WOLFLEADEROP3y ago
but would it be body parser causing all those issues?
Want results from more Discord servers?
Add your server