Home > database >  How to make a POST request to Twilio to call a number with urequests
How to make a POST request to Twilio to call a number with urequests

Time:01-11

I have recently starting doing things with a raspberry pi zero W and wanted to be able to call a number from it.

Unfortunately it seems very hard to use the normal Twilio library because the pi uses MicroPython so I have to use the raw API.

I also gathered that the API uses the content type x-www-form-urlencoded which urequests seems to have a hard time interacting with.

This is my code so far

url = f"https://api.twilio.com/2010-04-01/Accounts/{TWILIO_USER}/Calls.json"


def call() -> None:
    body = {"To": CALLING_NUMBER, "From": CALLER_NUMBER}
    response = urequests.post(url, json=body, auth=(TWILIO_USER, TWILIO_KEY))

    print("Status Code", response.status_code)
    print("JSON Response ", response.json())

however I get the error

{'code': 21201, 'more_info': 'https://www.twilio.com/docs/errors/21201', 'message': "No 'To' number is specified", 'status': 400}

I have tried a ton of stuff like url encoding the body myself, using it as the parameter for data, json.dumpsing it, nothing seemed to work. Any help would be greatly appreciated.

Please remember I am limited by the picos standard libraries.

CodePudding user response:

It seems I was close. All I had to do was copy paste urllib's urlencode and use it on body, then it ended up working.

  • Related