Getting raw body in route handlers (next 14)
Answered
Turkish Van posted this in #help-forum
Turkish VanOP
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.
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.
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
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.
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.
Answered by joulev
you can only consume the body once. so after you run
so get rid of that
req.json you can no longer run req.text. similarly if you have run req.text you can't run req.arrayBuffer for example.so get rid of that
const data = await req.json(). if you need json data, you can always doconst textBody = await req.text();
const data = JSON.parse(textBody);2 Replies
@Turkish Van 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.
you can only consume the body once. so after you run
so get rid of that
req.json you can no longer run req.text. similarly if you have run req.text you can't run req.arrayBuffer for example.so get rid of that
const data = await req.json(). if you need json data, you can always doconst textBody = await req.text();
const data = JSON.parse(textBody);Answer
@joulev you can only consume the body once. so after you run `req.json` you can no longer run `req.text`. similarly if you have run `req.text` you can't run `req.arrayBuffer` for example.
so get rid of that `const data = await req.json()`. if you need json data, you can always do
tsx
const textBody = await req.text();
const data = JSON.parse(textBody);
Turkish VanOP
Thank you so much! Was completely clueless about that and spent unnecessarily long. Appreciate you 🙂