[英]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.您也不希望将其他数据位、
Path
和Visibility
作为标头发送,它们应该是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.