Any way to access raw body? #93
-
I cannot for the life of me access the raw body of a serverless function to sign a webhook. |
Beta Was this translation helpful? Give feedback.
Answered by
leerob
Jan 11, 2022
Replies: 0 comments 1 reply
-
https://vercel.com/support/articles/how-do-i-get-the-raw-body-of-a-serverless-function import type { VercelRequest, VercelResponse } from '@vercel/node';
import type { Readable } from 'node:stream';
export const config = {
api: {
bodyParser: false,
},
};
async function buffer(readable: Readable) {
const chunks = [];
for await (const chunk of readable) {
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
}
return Buffer.concat(chunks);
}
export default async function (req: VercelRequest, res: VercelResponse) {
if (req.method === 'POST') {
const buf = await buffer(req);
const rawBody = buf.toString('utf8');
// Can do something here...
res.json({ rawBody });
} else {
res.setHeader('Allow', 'POST');
res.status(405).end('Method Not Allowed');
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
leerob
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://vercel.com/support/articles/how-do-i-get-the-raw-body-of-a-serverless-function