简体   繁体   中英

How to send multiple files with Python requests to FastAPI server?

I have the following endpoint in my FastAPI server:

@app.post("/submit")
async def submit(file1: List[UploadFile] = File(...), file2: List[UploadFile] = File(...), file3: List[UploadFile] = File(...)):
    # do something
    return 'hello'

It works as expected if I use Swagger UI. However, I cannot figure out how to send a POST request to this endpoint using Python requests module. Here is what I've tried:

import requests

headers = {
    'accept': 'application/json',
    'Content-Type': 'multipart/form-data',
}

files = [
    ('file1', open('file1.zip', 'rb')), 
    ('file2', open('file2.txt', 'rb')),
    ('file3', open('file3.rar', 'rb')),
]

response = requests.post('http://localhost:8000/submit', headers=headers, files=files)

print(response.text)

But I am getting an error:

{"detail":"There was an error parsing the body"}

You should either define each file as a separate parameter in the endpoint, eg, file1: UploadFile = File(...) , file2: UploadFile = File(...) , etc. (without using List ), or, preferably, define a List of files using files: List[UploadFile] = File(...) , as shown below (the below can be used to return the filenames of all uploaded files).

@app.post("/submit")
async def submitUPP(files: List[UploadFile] = File(...)):
    # do something
    return {"Uploaded Files:": [file.filename for file in files]}

On client side, you should send the list of files using the same key you gave in the endpoint (ie, files in the case above) for each file.

files = [
    ('files', open('file1.zip', 'rb')), 
    ('files', open('file2.txt', 'rb')),
    ('files', open('file3.rar', 'rb')),
]

You may also want to have a look at this and this answer.

Important

In addition to the above, one shouldn't set the Content-Type in the headers to multipart/form-data when using files, but rather let Python requests set the Content-Type , as in addition to multipart/form-data , Content-Type must include the boundary string used to separate each body part in the multipart payload). Please have a look at the answers here and here . Setting the Content-Type in the way you do is the reason for the error you encountered, ie, {'detail': 'There was an error parsing the body'} . Thus, you should avoid setting the Content-Type in the headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM