P粉4222270232023-08-17 12:40:36
(2018) The new Python requests library simplifies this process. We can use the 'files' variable to indicate that we want to upload a multi-part encoded file
url = 'http://httpbin.org/post' files = {'file': open('report.xls', 'rb')} r = requests.post(url, files=files) r.text
P粉5904283572023-08-17 11:32:38
If upload_file
refers to a file, use:
files = {'upload_file': open('file.txt','rb')} values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'} r = requests.post(url, files=files, data=values)
Then requests
will send a multipart form POST request body with the upload_file
field set to the contents of the file.txt
file.
The file name will be included in the mime header of the specific field:
>>> import requests >>> open('file.txt', 'wb') # 创建一个空的演示文件 <_io.BufferedWriter name='file.txt'> >>> files = {'upload_file': open('file.txt', 'rb')} >>> print(requests.Request('POST', 'http://example.com', files=files).prepare().body.decode('ascii')) --c226ce13d09842658ffbd31e0563c6bd Content-Disposition: form-data; name="upload_file"; filename="file.txt" --c226ce13d09842658ffbd31e0563c6bd--
Please note the filename="file.txt"
parameter.
If you need more control, you can use tuples as files
mapping values. The length of the tuple should be between 2 and 4. The first element is the file name, followed by the content, optionally including a mapping of the content-type header and other headers:
files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}
This will set an alternative filename and content type, omitting the optional headers.
If you want the entire POST request body to come from a file (no other fields are specified), do not use the files
parameter and POST the file directly as data
. You may also want to set a Content-Type
header, otherwise no header will be set. SeePython requests - POST data from a file.