简体   繁体   中英

How to make a POST request to Twilio to call a number with urequests

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.dumps ing it, nothing seemed to work. Any help would be greatly appreciated.

Please remember I am limited by the picos standard libraries.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM