Search code examples
pythondjangodjango-rest-frameworkwebhooksmollie

Django CSRF verification failed. Webhook payment system


I'm trying to implement a webhook for my payment system. I have a path to my webhook view, which is a simple definition where the provided id is printed.

The usage is like this

http://localhost:8000/api/mollie-webhook/?id=ExampleId

Path

# mollie webhook
path('api/mollie-webhook/', mollie_webhook, name='mollie_webhook'),

View

def mollie_webhook(request):
    id = request.POST['id']
    print(id)
    return JsonResponse(data={"response": "Success!"})

I'm getting the following error

CSRF verification failed. Request aborted.

Solution

  • Use the csrf_exempt decorator to mark the view as exempt from CSRF checks

    from django.views.decorators.csrf import csrf_exempt
    
    @csrf_exempt
    def mollie_webhook(request):
        id = request.POST['id']
        print(id)
        return JsonResponse(data={"response": "Success!"})