python requests upload folder example

1. Required environment

Requests Library

2. Send a single file in a single field

There is only one interface for sending files in requests, that is, using requests The files parameter of post. The request form is as follows:

url = ""
data = None
files = { ... }
r =, data, files=files)

The files parameter can accept many forms of data. The two most basic forms are:
Dictionary type
Tuple list type

2.1. files parameter of dictionary type

The official recommended dictionary parameter format is as follows:

 "field1" : ("filename1", open("filePath1", "rb")),
 "field2" : ("filename2", open("filePath2", "rb"), "image/jpeg"),
 "field3" : ("filename3", open("filePath3", "rb"), "image/jpeg", {"refer" : "localhost"})

The key of the dictionary is the field name when sending the post request, and the value of the dictionary describes the information of the file to be sent; As can be seen from the above, value can be 2 tuples, 3 tuples or 4 tuples;
The meaning of each field of this tuple is:

("filename", "fileobject", "content-type", "headers")
By default, the default value is used

In addition to the above usage forms, requests supports a more concise parameter form, as follows

 "field1" : open("filePath1", "rb")),
 "field2" : open("filePath2", "rb")),
 "field3" : open("filePath3", "rb"))

The equivalent effect of parameters in this form is as follows, where filename is the file name of filepath:

 "field1" : ("filename1", open("filePath1", "rb")),
 "field2" : ("filename2", open("filePath2", "rb")),
 "field3" : ("filename3", open("filePath3", "rb"))

Of course, you can also send a file request like this

 "field1" : open("filePath1", "rb").read())

The value of filename here is field1

2.2. files parameter of tuple list type

In fact, the form of tuple list is basically the same as that of dictionary, except that the packaging of the outermost layer is different; Within requests, the dictionary parameter form will eventually be converted to the tuple column form. The usage recommended on the official website is as follows:

 ("field1" : ("filename1", open("filePath1", "rb"))),
 ["field2" : ("filename2", open("filePath2", "rb"), "image/jpeg")],
 ("field3" : ("filename3", open("filePath3", "rb"), "image/jpeg", {"refer" : "localhost"}))

The sub items in the list can be tuples or lists; Similarly, the form of introduction is also supported here, as follows:

 ("field1" : open("filePath1", "rb"))), ##Filename uses the filename of filepath
 ("field2" : open("filePath2", "rb").read())) ##filename uses the key value, field2

3. Send multiple files in a single field [that is, when uploading files, it is set to multiple selection]

3.1 dictionary parameter form

 "field1" : [
   ("filename1", open("filePath1", "rb")),
   ("filename2", open("filePath2", "rb"), "image/png"),
   open("filePath3", "rb"),
   open("filePath4", "rb").read()

3.2 tuple list form

 ("field1" , ("filename1", open("filePath1", "rb"))),
 ("field1" , ("filename2", open("filePath2", "rb"), "image/png")),
 ("field1" , open("filePath3", "rb")),
 ("field1" , open("filePath4", "rb").read())

For the requests sent in the above two forms, all files will be in the same field, and the background service can obtain all file objects from field1 field

4. Send common data fields at the same time

The above description is about sending file content requests. Sometimes we need to send ordinary data fields while sending files. At this time, the ordinary data fields can be directly stored in the data parameter, as follows:

data = {"k1" : "v1"}
files = {
 "field1" : open("1.png", "rb")
r ="", data, files=files)

Original link:

Posted by GuitarheadCA on Fri, 15 Apr 2022 18:54:00 +0930