2017-07-08 58 views
0

我试图建立一个webhook,接收来自第三方服务Messagebird的JSON POST。在他们的文档,他们曾传出查询的例子:解析传入Webhook的JSON

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 

我的网络挂接正在使用Python内置Django的,这就是我在我的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) 

我的目标将JSON读入文件usrtxt,然后将这些字段映射到模型。我得到这个错误(部署在Heroku):

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

这是因为json.loads试图读取文件和第一像开始与GET?我需要跳过这一行吗?还是有另一种方式去解决这个问题?

回答

0

这看起来可能过于简单,但可以在您的webhook上添加@csrf_exempt装饰器。