Manza
Manza

Reputation: 2141

Django + RestFramework + twilio: Getitng status call back sid

I am trying something that I believe is so simple and I it works fine when i test my end point with postman, however when the site is up, is when i get "error"

These are my current settings:

twilio==6.29.1
Django==2.0.7
djangorestframework==3.9.4
Python 3.6.8

After sending an SMS with twilio:

client = Client(key1,key2)
message = client.api.account.messages.create(
    body= request.data["Body"],
    to= request.data["toNumber"],
    status_callback='https://myurl',
    from_= request.data["fromNumber"]
)

I have set up the call back

class SMSCallBack(APIView):
    parser_classes = (JSONParser,)
    def post(self, request, format=None):  
        print(request.GET)
        print(request.GET.getlist('SmsSid'))
        return Response({'xxx': 'xxx'})

However this is my respond in the logs

<QueryDict: {}>
[]            

If I try to do the same in postman I get

<QueryDict: {'SmsSid': ['asd']}>
['asd']

Now I am guessing with postman that Ihave set it correctly (POST, and add in params tab a value) as the console log from twilio is indicating that one of the multiple params they are sending is the SmsSid

Twilio debug console

I am pretty new with Django and python, so perhaps is there something i am missing when trying to get the incoming parameters?

When I try

request.POST.getlist('SmsSid')

My result is:

2019-07-15T05:15:25.735240+00:00 app[web.1]: 10.65.77.132 - - [15/Jul/2019:05:15:25 +0000] "POST /XXXX/smscallback/ HTTP/1.1" 415 100 "-" "TwilioProxy/1.1"
2019-07-15T05:15:25.738919+00:00 heroku[router]: at=info method=POST path="/XXXX/smscallback/" host=XXX.com request_id=3c5b81c2-003b-461a-8bd9-3e79d4545a9a fwd="3.94.8.12" dyno=web.1 connect=1ms service=783ms status=415 bytes=346 protocol=https

When I try in postman i get

[]

Upvotes: 1

Views: 237

Answers (1)

philnash
philnash

Reputation: 73057

Twilio developer evangelist here.

Twilio is going to make a POST request to your statusCallback endpoint. So the data will be in the request body, under request.POST not under request.GET.

Your code also shows that you are using the JSONParser to parse the incoming request. Twilio sends the request as application/x-www-form-urlencoded so you should be using the FormParser instead.

Let me know if that helps at all.

Upvotes: 3

Related Questions