1

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"}
0

1 Answer 1

3

You should either define each file as a separate parameter in the endpoint, e.g., 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 (i.e., 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, i.e., {'detail': 'There was an error parsing the body'}. Thus, you should avoid setting the Content-Type in the headers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.