sveltekit-stripe
sveltekit-stripe copied to clipboard
Update for changes in SvelteKit that remove `rawBody`
Since https://github.com/sveltejs/kit/pull/3384, rawBody
is no longer exposed in endpoints. I have not found a way to correctly pass the original request into Stripe's constructEvent
to verify the event signature. I'd love to know if you think that can be done and how.
Seems in hooks.js
handle()
function you can just use event.request
in order to get the original request coming in from the client
See https://github.com/sveltejs/kit/pull/3384
Hey, I'm also using Stripe's API and need the rawBody from an endpoints post
function. How do we get the raw request body now?
Hey, I'm also using Stripe's API and need the rawBody from an endpoints
post
function. How do we get the raw request body now?
I found this to work for me:
const _rawBody = await event.request.arrayBuffer();
const rawBody = toBuffer(_rawBody);
stripeEvent = stripe.webhooks.constructEvent(
rawBody,
stripeSignature,
process.env.STRIPE_WEBHOOK_SECRET
);
Hey, I'm also using Stripe's API and need the rawBody from an endpoints
post
function. How do we get the raw request body now?I found this to work for me:
const _rawBody = await event.request.arrayBuffer(); const rawBody = toBuffer(_rawBody); stripeEvent = stripe.webhooks.constructEvent( rawBody, stripeSignature, process.env.STRIPE_WEBHOOK_SECRET );
I had the need of rawbody recently and have used the same pattern.
You can remove the toBuffer
function with a Buffer.from
call. Something like this
const rawBody = Buffer.from(await request.arrayBuffer());
Does anyone has a solution ... None of these worked for me ... 😢