Вы не должны явно указывать «multipart/form-data». Он переписывает всю другую часть набора заголовков по запросам («multipart/form-data; border = 4b9 ...»,). Нет необходимости устанавливать заголовок, запросы будут делать это за вас. Вы можете увидеть заголовки запросов (request.headers) в приведенном ниже примере. Вы можете видеть, что
import requests
endpoint = "http://httpbin.org/post"
r = requests.post(
endpoint,
files={"config": ("conf.ttl", open("conf.ttl", "rb"), "text/turtle")}
)
print r.request.headers
print r.headers
print r.text
дает:
{'Content-Length': '259', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.10.0', 'Connection': 'keep-alive', 'Content-Type': 'multipart/form-data; boundary=4b99265adcf04931964cb96f48b53a36'}
{'Content-Length': '530', 'Server': 'nginx', 'Connection': 'keep-alive', 'Access-Control-Allow-Credentials': 'true', 'Date': 'Fri, 20 May 2016 20:50:05 GMT', 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json'}
{
"args": {},
"data": "",
"files": {
"config": "curl -X POST \"endpoint\" -H 'Content-Type: multipart/form-data' -F \"[email protected]\"\n\n"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "259",
"Content-Type": "multipart/form-data; boundary=4b99265adcf04931964cb96f48b53a36",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.10.0"
},
"json": null,
"origin": "84.92.144.93",
"url": "http://httpbin.org/post"
}
Где, как ваш код с явным заголовком дает ошибку на тот же URL.
{'Content-Length': '259', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'User-Agent': 'python-requests/2.10.0', 'Connection': 'keep-alive', 'Content-Type': 'multipart/form-data'}
{'Date': 'Fri, 20 May 2016 20:54:34 GMT', 'Content-Length': '291', 'Content-Type': 'text/html', 'Connection': 'keep-alive', 'Server': 'nginx'}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
Не уверен, но я считаю, что вы хотите изменить '' config'' '' file''. – DeepSpace