-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to access a rawBody in an endpoint #10841
Comments
@jesuscovam apologies for prematurely transferring the original issue to a discussion. After a bit more digging around, I found out SvelteKit is creating a new I currently lack the knowledge to know if this is the cause of the issue. If it is, I wonder if using a EDIT: found an example SvelteKit repository implementing Stripe's webhook https://github.com/supabase-community/sveltekit-subscription-payments/blob/main/src/routes/api/webhooks/%2Bserver.ts |
Don't you worry I saw you had a new release this week. I tried the example from supabase but still got the error, I added the new method into the repo |
I finally got down to signing up to Stripe and trying to reproduce the issue with the Stripe CLI. It works perfectly with either import { json, text } from "@sveltejs/kit";
import { env } from "$env/dynamic/private";
import Stripe from "stripe";
const stripe = new Stripe(env.STRIPE_SECRET_KEY, {
apiVersion: "2023-08-16",
});
export const POST = async ({ request }) => {
const sig = request.headers.get("stripe-signature");
const webhookSecret = env.STRIPE_WEBHOOK_SECRET;
let event: Stripe.Event;
try {
if (!request.body || !sig || !webhookSecret) {
return text("Client Error", { status: 400 });
}
// this works too!
// const payload = await request.text();
const payload = Buffer.from(await request.arrayBuffer());
event = await stripe.webhooks.constructEventAsync(payload, sig, webhookSecret);
} catch (err: any) {
console.error(`❌ Error message: ${err.message}`);
return text(`Webhook Error: ${err.message}`, { status: 400 });
}
return json({ event });
}; |
For anyone else coming here looking for answers: If you input the wrong webhook signing secret, the error you get is about the request body. Really confusing 🫤 Make sure you use the local signing secret (printed in the terminal) if you're forwarding webhook events to your local app. |
Discussed in #10832
Originally posted by jesuscovam October 5, 2023
Describe the bug
To validate a request from stripe, I have to provide them the rawBody of the request, but every try throws an error.
I have tried other solutions that I read in other issues that worked for some users but I couldn't make it work
Solution with text: #3384 (comment)
Solution with buffer: #3384 (comment)
I think we have the same error #10339
Reproduction
pnpm dev
this sveltekit app repohave installed the stripe-cli and have an user to test it download stripe cli
do the login for the stripe with
stripe login
connect the cli with the server
stripe listen --forward-to http://localhost:5173/api/with-buffer
in a another terminal run a stripe event, the one I use is payment.intent
stripe trigger payment_intent.succeeded
you can also test the other method with
stripe listen --forward-to http://localhost:5173/api/with-text
Logs
System Info
Severity
blocking an upgrade
Additional Information
No response
The text was updated successfully, but these errors were encountered: