@@ -21,9 +21,8 @@ export * from '@clerk/backend/webhooks';
21
21
* @example
22
22
* ```typescript
23
23
* import { verifyWebhook } from '@clerk/express/webhooks';
24
- * import express from 'express';
25
24
*
26
- * app.post('/api/webhooks', express.raw({ type: 'application/json' }), async (req, res) => {
25
+ * app.post('/api/webhooks', async (req, res) => {
27
26
* try {
28
27
* const evt = await verifyWebhook(req);
29
28
* // handle event
@@ -40,8 +39,23 @@ export async function verifyWebhook(req: ExpressRequest, options?: VerifyWebhook
40
39
const webRequest = incomingMessageToRequest ( req ) ;
41
40
// Cloning instead of implementing the body inside incomingMessageToRequest
42
41
// to make it more predictable
42
+ // we must pass in body as string not as an Object or Buffer
43
+ let serializedBody : string ;
44
+ if ( typeof req . body === 'string' ) {
45
+ serializedBody = req . body ;
46
+ } else if ( Buffer . isBuffer ( req . body ) ) {
47
+ serializedBody = req . body . toString ( 'utf8' ) ;
48
+ } else if ( req . body === undefined || req . body === null ) {
49
+ serializedBody = '' ;
50
+ } else {
51
+ try {
52
+ serializedBody = JSON . stringify ( req . body ) ;
53
+ } catch ( error ) {
54
+ throw new Error ( `Failed to serialize request body: ${ error instanceof Error ? error . message : 'Unknown error' } ` ) ;
55
+ }
56
+ }
43
57
const clonedRequest = new Request ( webRequest , {
44
- body : req . body ,
58
+ body : serializedBody ,
45
59
} ) ;
46
60
return verifyWebhookBase ( clonedRequest , options ) ;
47
61
}
0 commit comments