#Getting raw body in route handlers (next 14)

4 messages · Page 1 of 1 (latest)

foggy tapir
#

I'm trying to integrate a local payment gateway (not stripe), that demands the raw body for validating the webhook.

I first tried using await req.text(), as this seems to be the natural solution to this.


export async function POST(req: Request) {
  const data = await req.json();
  try {
    const rawBody = await req.text();
    console.log("rawbody: " + rawBody);
  } catch (err) {
    console.log("req.text error");
    console.log(err?.message!);
  }
}

But that gives me this error:

req.text error
Body is unusable

I also tried to tweak the config for this endpoint so that the parsing is disabled, by doing this.

export const config = {
  api: {
    bodyParser: false,
  },
};

It seems like this is deprecated now, I get the below warning. And the link does not have any parsing-related config options.

⚠ Page config in C:{project}\app\api\cf-webhook-offload\route.ts is deprecated. Replace export const config=… with the following:
Visit https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config for more information.

The req.json() parses to an object that I expect from the webhook properly.

Is there any way I turn this into rawBody?

This is supposed to be straightforward, and I feel like I'm missing something trivial here.

Thanks.

Learn about how to configure options for Next.js route segments.

gilded sluiceBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

spice quarry
foggy tapir