About Reading Posted JSON Data

You can read JSON data posted to a Django view like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import json
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def webhook_view(request):
    if request.method == "POST":
        try:
            data = json.loads(request.body)
        except json.JSONDecodeError:
            return HttpResponseBadRequest("Sorry! Malformed JSON.")
        # process data...
        return HttpResponse("All done.")
    return HttpResponseBadRequest("Only POST requests allowed.")

Tips and Tricks Programming Development Django 5.x Django 4.2 Django 3.2 JSON