Django 解析 json(来自 Webhooks)

Django parsing json (from Webhooks)

我正在尝试将从 Mailchimp 收到的一些 json 解析为我的 Django 应用程序中的 Webhook。格式将如下所示:

{
  "type": "subscribe",
  "fired_at": "2009-03-26 21:35:57",
  "data": {
    "id": "8a25ff1d98",
    "list_id": "a6b5da1054",
    "email": "api@mailchimp.com",
    "email_type": "html",
    "ip_opt": "10.20.10.30",
    "ip_signup": "10.20.10.30",
    "merges": {
      "EMAIL": "api@mailchimp.com",
      "FNAME": "Mailchimp",
      "LNAME": "API",
      "INTERESTS": "Group1,Group2"
    }
  }
}

我尝试了以下方法:

def marketing_email_handler(request):
    if request.method == 'POST':

          reqtype = request.body['type']
          reqdata = request.body['data']
          reqemail = request.body['data']['email']

我收到错误 'byte indices must be integers'。

我尝试将其更改为以下内容,但这会导致错误“类型错误:'int' 对象不可订阅”。做 request.json() 也会导致错误

      reqtype = request.body[0]['type']
      reqdata = request.body[0]['data']
      reqemail = request.body[0]['data']['email']

我也试过了(如 TypeError: byte indices must be integers or slices, not str 所建议)

json.loads(request.body)

但我收到以下信息:'JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)'

解决了这个问题 - 这个问题是 'request.body' - 这应该是 request.POST