简体   繁体   English

如何修复 python 对 Twilio 的 POST 请求上的“不支持的媒体类型”错误

[英]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 .我一直在关注 Twilio 的快速入门页面,了解他们的功能 API

I am stuck at the part where I am supposed to manually upload the Function JS file.我被困在我应该手动上传 Function JS 文件的部分。

Their POST examples use cURL and node.js but I am using Python 3.6:他们的 POST 示例使用 cURL 和 node.js 但我使用的是 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 .在这两个示例中,它们都将内容类型声明为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.那 URL 抛出404所以我去 Twilio 的错误字典中挖掘,但没有列出该代码。 Furthermore, application/javascript is absent from their supported media types page.此外,他们支持的媒体类型页面中没有application/javascript

Am I uploading the file incorrectly?我是否错误地上传了文件? Or is their tutorial wrong?还是他们的教程错了?

Twilio developer evangelist here. Twilio 开发人员布道师在这里。

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.我认为您可能已将 curl 请求的某些部分翻译为使用请求发出的requests的错误部分,我认为这是导致问题的原因。 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.您不想将请求类型设置为想要成为您正在上传的文件类型的application/javascript You can set this as part of the files tuple.您可以将其设置为files元组的一部分。

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.您也不希望将其他数据位、 PathVisibility作为标头发送,它们应该是data的一部分,因此它们成为请求正文的一部分。

Try something like this instead:尝试这样的事情:

upload_url = f'https://serverless-upload.twilio.com/v1/Services/{sub_service_sid}/Functions/{sub_function_sid}/Versions'

files = { 'file': ('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.让我知道这是否有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 request.post 不支持的媒体类型错误 - Unsupported Media type error for request.post 如何使用请求修复Python中的'415 Unsupported Media Type'错误 - How to fix '415 Unsupported Media Type' error in Python using requests “标题”:“不支持的媒体类型”,“状态”:从 python 请求 API 时出现 415 错误 - “title”:“Unsupported Media Type”,“status”:415 error when request API from python 如何修复不支持的操作数类型错误? - How To Fix Unsupported Operand Type Error? 如何修复 [类型错误:+= 不支持的操作数类型:'builtin_function_or_method' 和 'str'] Python 中的错误 - How to fix [Type Error: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'str'] error in Python 如何修复此错误:TypeError: unsupported operand type(s) for *: 'float' and 'function' in python - How do I fix this error: TypeError: unsupported operand type(s) for *: 'float' and 'function' in python 如何修复类型错误:+ 的不支持的操作数类型:'function' 和 'int'? - How to fix the Type error: unsupported operand type(s) for +: 'function' and 'int'? 知道如何在 python 中修复此错误“TypeError: unsupported operand type(s) for -: 'list' and 'list'”吗? - Any idea how to fix this error “TypeError: unsupported operand type(s) for -: 'list' and 'list'” in python? Python GET 请求 - 415 不支持的媒体类型 - Python GET requests - 415 unsupported media type 如何在 Python 中修复“+ 不支持的操作数类型:‘int’和‘list’” - How to fix "unsupported operand type(s) for +: 'int' and 'list'" in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM