Skip to content

Any way to access raw body? #93

Locked Answered by leerob
roycejewell asked this question in Help
Discussion options

You must be logged in to vote

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...

Replies: 0 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by leerob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants