How do I get the rawBody on Vercel Functions? #5213
-
I need to receive the Any advice on what I need to do? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 11 replies
-
With micro import { NowRequest, NowResponse } from '@vercel/node';
import { buffer } from 'micro'; // <--------
import { environment } from '../_config/env';
import { cors } from '../_config/cors';
import { stripeService } from '../_services/stripe.service';
const handler = async (req: NowRequest, res: NowResponse) => {
if (req.method === 'POST') {
// verify signature
const sig = req.headers['stripe-signature'];
const buf = await buffer(req); // <---- HERE
const body: string = buf.toString(); // <---- HERE
const hookSecret = environment.stripe.hookSecret;
// take event
stripeService
.constructEvent(body, sig, hookSecret)
.then((event: any) => event)
// stuff
.finally(() => res.status(200).send({ received: true }));
} else {
res.status(404).end()
}
};
export default cors(handler); |
Beta Was this translation helpful? Give feedback.
-
I'm having the exact same problem in the exact same use case. However, Im not using Next.JS so I can't add that config option to my serverless function. Is there any way to access the raw body in Vercel, or do I have to host my functions elsewhere? |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I'm having the exact same problem. Is there no |
Beta Was this translation helpful? Give feedback.
-
Update 2024: This is now much easier with the Web API signature support. export async function POST(request: Request) {
const rawBody = await request.text()
return Response.json({ rawBody })
} |
Beta Was this translation helpful? Give feedback.
Update 2024: This is now much easier with the Web API signature support.