Skip to content

We need to be able to get Raw body's request data from API #10339

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

Closed
alanxp opened this issue Jul 7, 2023 · 3 comments
Closed

We need to be able to get Raw body's request data from API #10339

alanxp opened this issue Jul 7, 2023 · 3 comments

Comments

@alanxp
Copy link

alanxp commented Jul 7, 2023

Describe the problem

When we get a request from our API, i can't seem to get the raw body request that stripe needs for the webhooks so i can't process payments lol. Been very frustrating, already searched for days, gpt + bard + forums and nothing. Stripe needs the body request untouched without any sveltekit's processing on it.

So using request.text() it wont help since it already has some processing.

Describe the proposed solution

Be able to get request.rawData

Alternatives considered

The only idea to make it work is to use a custom node server but that's just not the ideal

Importance

i cannot use SvelteKit without it

Additional Information

No response

@elliott-with-the-longest-name-on-github
Copy link
Contributor

Please provide an actual reproduction. What you're describing is possible, so we can't help without knowing what's going wrong.

@elliott-with-the-longest-name-on-github
Copy link
Contributor

I'm closing this as it has no reproduction. If you're still having this issue, please open a new one with a reproduction conforming to the requirements in the issue template.

@LamechIsrael
Copy link

I need help on this as well. All the packages I use return some sort of error, when trying to get the raw-body. The buffer from micro returns "Invalid body." And getRawBody returns "Argument stream must be a stream." Whatever that means.

import { NextApiRequest, NextApiResponse } from "next";
import Stripe from "stripe";
import { headers } from "next/headers";
import getRawBody from "raw-body";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
  apiVersion: "2023-10-16",
});

const endpointSecret = process.env.WEBHOOK_SECRET as string;

// Make sure to add this, otherwise you will get a stream.not.readable error
export const config = {
  api: {
    bodyParser: false,
  },
};

export async function POST(req: NextApiRequest, res: NextApiResponse) {
  try {
    const headersList = headers();
    const signature = headersList.get("stripe-signature") as string;


    const rawBody = await getRawBody(req);
  } catch (error) {
    console.error(error);
 
    return Response.json({
      key: "message",
      status: 500,
      message:
        "Internal Server Error. Check /api/payment/stripe-webhook-endpoint.ts",
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants