1

I'm trying to build a webhook that receives JSON being POSTed from a third party service, Messagebird. In their documentation they have an example of an outgoing query:

GET http://your-own.url/script
    ?id=e8077d803532c0b5937c639b60216938
    &recipient=31642500190
    &originator=31612345678
    &body=This+is+an+incoming+message
    &createdDatetime=2016-05-03T14:26:57+00:00

My webhook is being built with Python in Django, and this is what I have in my views.py:

from django.shortcuts import render
from django.views.decorators.http import require_POST
from django.http import HttpResponse
from .models import UserText

@require_POST
def webhookmb(request):
    usrtxt = json.loads(request.body.decode("utf-8"))

    UserText.objects.create(
        id = usrtxt['id']
        recipient = usrtxt['recipient']
        originator = usrtxt['originator']
        body = usrtxt['body']
        createdDatetime = usrtxt['createdDatetime']
    )

    return HttpResponse(200)

My goal is to read the JSON into a file usrtxt and then map those fields to a model. I'm getting this error (deployed on heroku):

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Is this because json.loads is trying to read the file and the first like starts with GET? Do I need to skip this line? Or is there another way to go about this?

Grokify
  • 15,092
  • 6
  • 60
  • 81
hackerman
  • 1,221
  • 3
  • 17
  • 38

1 Answers1

0

This might seem overly simple but try adding @csrf_exempt decorator to your webhook.

Lee
  • 87
  • 11