twillio throws AttributeError: 'str' object has no attribute 'get' in Django-python

twillio throws AttributeError: 'str' object has no attribute 'get' in Django-python

当我将 twilio 用于 whatsapp 时,它会收到消息。但是,当我尝试使用 MessagingResponse 发送响应时,它会产生此错误。 AttributeError: 'str' object has no attribute 'get'

我使用的代码是

def twillioRequest(request):
    msg = request.POST['Body']
    print(msg)
    
    resp = MessagingResponse()
    resp.message('Ok')
    print(str(resp))
    return str(resp)

直到 print(str(resp)) 这个语句工作正常。此代码部署在 django 服务器中,我已将 twilio 与我的 URL 连接起来。这个错误的原因是什么?

你应该使用 HttpResponse,像这样:

from django.http import HttpResponse

def twillioRequest(request):
    msg = request.POST['Body']
    print(msg)
    
    resp = MessagingResponse()
    resp.message('Ok')
    print(str(resp))
    return HttpResponse(str(resp))