-
Feature requestAs per @timneutkens request, this is an RFC to add Is your feature request related to a problem? Please describe.Currently, there is no way to use both a body parser and recuperate the raw body of a request. This makes things like securing github webhooks, which rely on calculating a hash based on a raw body, difficult in NextJS. Describe the solution you'd likeThe solution I proposed in the PR above is to add a raw body field to NextApiRequest. Describe alternatives you've consideredThe alternative we're currently using is deploying a separate microservice to handle our github webhook. It could be that this feature is outside the scope of nextjs, in which case the separate microservice makes sense. |
Beta Was this translation helpful? Give feedback.
Replies: 14 comments 21 replies
-
Would disabling the This allows you to read the body however you'd like. |
Beta Was this translation helpful? Give feedback.
-
Hey! This definitely helps - we were hoping to use the body parser as the body is json, but what we can do as a temporary measure is disable the body parser and then parse the JSON ourselves. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I would add to this, I had the same issue with stripe webhooks. |
Beta Was this translation helpful? Give feedback.
-
The stringify solution did not work for me. Also, after disabling the bodyParser, |
Beta Was this translation helpful? Give feedback.
-
Are there any future plans/alternate ways of maybe defining interceptors at the config for handling hashes/security on a per API call? I'm sure many are writing wrapper methods like we are to handle this, while requiring the developer to also disable body parsing each time to work properly. https://github.com/jhookom/nextjs-oc-api/blob/main/pages/api/checkout/%5B...event%5D.ts#L6 https://github.com/jhookom/nextjs-oc-api/blob/main/lib/oc-catalyst-next.ts#L247 ` |
Beta Was this translation helpful? Give feedback.
-
disabling the parser means that reg.body will be undefined
|
Beta Was this translation helpful? Give feedback.
-
Hi pulling my hair out here trying to make this Stripe Webhook work. Tried all of the above! Secret is fine. Cant pass JSON req into raw as I am still getting error - What am I missing here?? TIA
|
Beta Was this translation helpful? Give feedback.
-
Just stumbled upon this issue and got it to work with the In my use case, verifying stripe webhooks, I don't need to parse the body. So I'm not sure this solves all use cases but since many others on this thread mentioned stripe I decided to share it. import getRawBody from 'raw-body';
export default async function handler(req, res) {
const rawBody = await getRawBody(req);
const endpointSecret = ...;
const signature = req.headers['stripe-signature'] ?? '';
const event = stripe.webhooks.constructEvent(
rawBody,
signature,
endpointSecret
);
}
export const config = {
api: {
bodyParser: false,
},
}; |
Beta Was this translation helpful? Give feedback.
-
If none of the above worked for you, you may try a solution from CodeDaily Disable the
Use micro by Vercel to get the body:
(I also need the parsed body, so after checking the signature, I do |
Beta Was this translation helpful? Give feedback.
-
I also had this problem trying to parse the Stripe webhook in Next.js. I resolved it by using body-parser's First I used a generic middleware function to help with applying middleware to individual routes: // ./utils/middleware.utils.ts
export function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: Function) {
return new Promise((resolve, reject) => {
fn(req, res, (result: Object) => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
} I disabled Next.js's implementation of body-parser and implemented // ./pages/api/webhooks.ts
// this disables bodyParser
export const config = {
api: {
bodyParser: false,
}
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
// use bodyParser.raw() as middleware instead
await runMiddleware(req, res, bodyParser.raw({ type: 'application/json' }))
try {
const signature = req.headers['stripe-signature']
const event = stripe.webhooks.constructEvent(req.body, signature, process.env.STRIPE_WEBHOOKS_SECRET)
res.status(200).json({})
// TODO: Handle event
} catch (e) {
return res.status(400).json({ error: 'Webhook error' })
}
} Using // Stripe example from their docs
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
//
}) |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Found this thread while working on stripe webhook. I'll add the solution i used, I found a simple way to solve this with the raw-body package. Don't know if is better than leerob's suggestion above, I suggest checking it out as well.
|
Beta Was this translation helpful? Give feedback.
-
This worked for me, for handling a stripe webhook in a route handler. export async function POST(request) {
const rawBody = await request.text()
// ...
} No special configuration needed. (Next |
Beta Was this translation helpful? Give feedback.
-
It worked only on local listener. not on live link. Error is- Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? Learn more about webhook signing and explore webhook integration examples for various frameworks at https://docs.stripe.com/webhooks/signature It is nextjs 15.3.3 and app router. |
Beta Was this translation helpful? Give feedback.
This worked for me, for handling a stripe webhook in a route handler.
No special configuration needed. (Next
13.5
app dir)