sveltekit-stripe icon indicating copy to clipboard operation
sveltekit-stripe copied to clipboard

Update for changes in SvelteKit that remove `rawBody`

Open brunobely opened this issue 3 years ago • 6 comments

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.

brunobely avatar Feb 05 '22 18:02 brunobely

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

evdama avatar Feb 17 '22 18:02 evdama

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?

WeAreELIC avatar Mar 08 '22 01:03 WeAreELIC

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
    );

kylebuildsstuff avatar Mar 13 '22 14:03 kylebuildsstuff

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.

kvetoslavnovak avatar Jun 19 '22 07:06 kvetoslavnovak

You can remove the toBuffer function with a Buffer.from call. Something like this

const rawBody = Buffer.from(await request.arrayBuffer());

fcrozatier avatar Nov 22 '22 18:11 fcrozatier

Does anyone has a solution ... None of these worked for me ... 😢

pierre-H avatar Jan 05 '23 13:01 pierre-H