pythonpostpython-requeststwiliomedia-type

How to fix 'Unsupported media type" error on python POST request to Twilio


I have been following Twilio's Quickstart page for their Functions API.

I am stuck at the part where I am supposed to manually upload the Function JS file.

Their POST examples use cURL and node.js but I am using Python 3.6:

# Manually upload the subscription function file
upload_url = f'https://serverless-upload.twilio.com/v1/Services/{sub_service_sid}/Functions/{sub_function_sid}/Versions'
function_request = requests.post(
                                upload_url,
                                files    = {'subscription_function_file': open('subscriptionFunction.js', 'rb')},
                                auth     = (account_sid, auth_token),
                                headers  = {
                                    'content-type': 'application/javascript',
                                    'path': '/subscription-function',
                                    'visibility': 'public'
                                }
                            )

In both examples, they declare the content type as application/javascript. However, I get this error when I do the same:

{"status":415,"message":"Unsupported media type","detail":"The server does not support the media type transmitted in the request.","code":20415,"moreInfo":"https://www/twilio.com/docs/errors/20415"}

That URL throws a 404 so I went digging in Twilio's Error Dictionary but that code is not listed. Furthermore, application/javascript is absent from their supported media types page.

Am I uploading the file incorrectly? Or is their tutorial wrong?


Solution

  • Twilio developer evangelist here.

    I think you may have translated some of the curl request to the wrong parts of a request made with requests and I think this is causing the issue. You don't want to set the request type to be application/javascript that wants to be the type of the file you are uploading. You can set this as part of the files tuple.

    You don't want to send the other bits of data, Path and Visibility as headers either, they should be part of the data so they become part of the request body.

    Try something like this instead:

    upload_url = f'https://serverless-upload.twilio.com/v1/Services/{sub_service_sid}/Functions/{sub_function_sid}/Versions'
    
    files = { 'Content': ('subscriptionFunction.js', open('subscriptionFunction.js', 'rb'), 'application/javascript') }
    
    function_request = requests.post(
                                    upload_url,
                                    files    = files,
                                    auth     = (account_sid, auth_token),
                                    data     = {
                                        'Path': '/subscription-function',
                                        'Visibility': 'public'
                                    }
                                )
    

    Let me know if that helps.