Reputation: 6597
In the FAST API documentation, the below example is listed as a reference how to pass additional parameters in a post request when file is transmitted.
https://fastapi.tiangolo.com/tutorial/request-forms-and-files/
from fastapi import FastAPI, File, Form, UploadFile
app = FastAPI()
@app.post("/files/")
async def create_file(
file: bytes = File(...), token: str = Form(...)
):
When I make a post request as below I get:
requests.post(URL, files={'file': hdf5_file, 'token': 'teststring'})
422 Unprocessable Entity
{'detail': [{'loc': ['body', 'model_str'],
'msg': 'str type expected',
'type': 'type_error.str'}]}
Why is token not recognized as a string? It's not clear from the documentation how a post request that mixes forms with files would need to look like.
Upvotes: 1
Views: 1097
Reputation: 32293
For requests
use the parameter files
to upload files, and data
for sending extra form data. In this case, everything will be sent as different parts with content-type multipart/form-data
:
requests.post(URL, files={'file': hdf5_file}, data={'token': 'teststring'})
Upvotes: 2