以下代码基本上是由邮递员生成的。我从标题中删除了“ Content-Type”,因为我知道这是不需要的。我也概括了URL路径等。但是,我收到一条错误消息:b'{“消息”:“发生错误。详细信息:将MIME多部分正文部分写入输出流时出错。”}'
但是,当我从Postman运行该请求时,该请求确实起作用。我在做什么错?
import requests
url = "https://website.com/api/inputs/file/upload/Test/12/"
payload = {}
files = [
('', open('C:/Users/jmas/Documents/Demo/test.csv','rb'))
]
headers = {
'Authorization': 'Bearer 0eE6r7DWAIriN6gBR1-5WJI-
9ZsfgE13JEthzuSXKQ9A05sbt5xdn8cAcV3Sz16D4thYNBcr6dQGdIcivSOpo-
0dz7tAVP19trL2bwQtQez6FyzZqJFqPQrHm7fLee9eEr5GSpth0JfeqV5Gt7z_juqB3dHDBINu1oxh9G-
pF8VuSRpUkAOujSMS0RysE1aptVqU1wAXLTXnKxUlDJLpTuQMCQGnFwqNvUWx3mDQ9xh4pw-ZaKw8TMvWaYgtmd1Z-
oAp2IgvP9bwV5pv5izyuUWIfaZyP0mqYlZu2'
}
response = requests.request("POST", url, headers=headers, data = payload, files = files)
print(response.text.encode('utf8'))
print(response.status_code)
您的有效负载只是一个空字典。删除data = payload
您正在将有效负载与请求一起传递,但是此词典为空。
我想您需要这样的东西:
url = 'https://website.com/api/inputs/file/upload/Test/12/'
files = {'': open('C:/Users/jmas/Documents/Demo/test.csv', 'rb')}
headers = {
'Authorization': 'Bearer 0eE6r7DWAIriN6gBR1-5WJI-9ZsfgE13JEthzuSXKQ9A05sbt5xdn8cAcV3Sz16D4thYNBcr6dQGdIcivSOpo -0dz7tAVP19trL2bwQtQez6FyzZqJFqPQrHm7fLee9eEr5GSpth0JfeqV5Gt7z_juqB3dHDBINu1oxh9G -pF8VuSRpUkAOujSMS0RysE1aptVqU1wAXLTXnKxUlDJLpTuQMCQGnFwqNvUWx3mDQ9xh4pw - ZaKw8TMvWaYgtmd1Z - oAp2IgvP9bwV5pv5izyuUWIfaZyP0mqYlZu2'
}
response = requests.post(url, headers=headers, files=files)
print(response)
您的标头格式似乎不遵循pep8规则,请尝试对其进行更改以使其格式正确。