When the signature never matches: the raw body problem
Most signature mismatches happen because the body was parsed and re-serialized, so the bytes differ from what was signed. Keep the bytes from before parsing.
What the raw body is
The raw body is the exact bytes of the HTTP request body, not the object a JSON parser produced. Signatures cover those bytes, so a single changed space produces a completely different HMAC.
These two strings represent the same object, but their signatures differ.
{"id":"evt_1","amount":4900}
{ "id": "evt_1", "amount": 4900 }Five common mutations
| Mutation | What changes | Where it happens |
|---|---|---|
| JSON re-serialization | whitespace, key order, number format | framework body parser |
| CRLF ↔ LF | line-break bytes | proxies, gateways, text-mode forwarding |
| Added trailing newline | final \n | editors, curl -d, copying logs |
| Unicode escaping | é ↔ \u00e9 | language JSON libraries |
| Character encoding | UTF-8 ↔ legacy code pages | old proxies, wrong Content-Type |
Any one of them makes the recomputed signature differ from the header. Far more often than not the signing logic is correct and the input changed.
Keeping the raw body by stack
| Stack | How to keep the raw body |
|---|---|
| Express | apply express.raw({ type: "*/*" }) to the webhook route only |
| Next.js App Router | const raw = await request.text() and verify that string |
| FastAPI | read await request.body() first |
| Flask | request.get_data() before parsing |
| Go | io.ReadAll(r.Body) and verify those bytes |
A global express.json() registered before the webhook route has already consumed the raw body. Scope the raw parser to the webhook path and mind the middleware order.
Diagnostic order
- Check the byte count and line endings of the received body.
- Keep the header fixed and retry verification with CRLF→LF or trailing whitespace removed.
- If it is JSON, also try the parsed-and-re-serialized form.
- Whichever variant matches tells you which layer rewrote the body; fix that layer.
Matching a variant in production is first aid, not a fix. Preserve the original bytes and use variant matching only while diagnosing.
The body comparison tool shows the first differing byte between two bodies.
Open the signature verifierFrequently asked questions
Does logging the body help debugging?
It helps, with care. Copying from logs can change line endings or whitespace, and personal data may be captured. Log the byte count and a hash alongside it.
What about Content-Encoding: gzip?
The sender signs the uncompressed body. The receiver must verify the decompressed bytes and confirm decompression did not alter them.
It works locally but fails in production. Why?
A proxy or API gateway in front of the app may rewrite the body or headers. Confirm that it forwards bytes unchanged, especially character and transfer encodings.
This guide follows the signing schemes published by GitHub, Stripe, Slack, Shopify and Toss Payments.
← Guides