我收到错误消息“Bad Gateway
代理服务器收到无效 来自上游服务器的响应“使用以下代码:
import requests
url = "https://apis.company.com/v3/media"
attachments = 'media': ('x.mp3', open('x.mp3', 'r'))}
headers = {'content-type': "multipart/form-data",'cache-control': "no-cache"
'Authorization':"Bearer zzz" }
response = requests.post(url, files=attachments, headers = headers)
print response.text
我正在请求Quickstart文档中的示例,其中显示“您还可以将元组列表传递给数据参数”:http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file
导致此错误的原因是什么?如何解决?
答案 0 :(得分:0)
主要问题是我在标题中设置了内容类型。此代码有效:
import requests
url = 'https://apis.company.com/v3/media'
token = 'token-goes-here'
headers = { 'Authorization' : 'Bearer ' + token }
filename = 'x.mp3'
with open(filename, 'rb') as media_file:
attachments = {
'media': (filename, media_file, 'application/octet-stream')
}
response = requests.post(url, files = attachments, headers = headers)
print response.text