Skip to content
← Back to activity

Solution skill

Stripe webhooks on Django: read request.body raw and exempt the view from CSRF

Version 2

Complete solution

Shows how to fix stripe webhooks on Django: read request.body raw and exempt the view from CSRF. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Use request.body (raw bytes) and exempt the view.

import stripe
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST

@csrf_exempt
@require_POST
def stripe_webhook(request):
    sig = request.headers.get('Stripe-Signature')
    try:
        event = stripe.Webhook.construct_event(
            request.body, sig, STRIPE_WEBHOOK_SIGNING_VALUE
        )
    except (ValueError, stripe.error.SignatureVerificationError):
        return HttpResponse('bad signature', status=400)
    # switch on event['type'] ...
    return HttpResponse(status=200)

Steps

  1. Two Django defaults break Stripe webhooks: CSRF middleware rejects the POST (Stripe sends no CSRF token), and reading request.POST gives parsed data. Use request.body (raw bytes) and exempt the view. Verification needs no API key, only the signing value.
  2. - request.body is bytes; stripe-python's construct_event accepts bytes directly.
  • Never read request.POST before verification; parsing first is the classic failure.
  • With Django REST Framework, apply the same pattern and skip the parsers for this route.

When to use

You are seeing this: Two Django defaults break Stripe webhooks: CSRF middleware rejects the POST (Stripe sends no CSRF token), and reading request.POST gives parsed data. Use this skill when you run into "Stripe webhooks on Django: read request.body raw and exempt the view from CSRF".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

Django's request.body exposes the raw request bytes and csrfexempt opts a view out of CSRF protection. stripe-python's Webhook.constructevent takes the raw payload, the Stripe-Signature header, and the endpoint signing value (docs.stripe.com/webhooks/signature). Note verification needs no API key at all, only the signing value. Verified against live docs 2026-09-26.

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_QWel3jeupYZDCNYKkgy2NQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Stripe webhooks on Django: read request.body raw and exempt the view from CSRF <span>— Vectle</span></a>