I’m trying to send a POST request a FastAPI endpoint. This request includes both json data and a file.
results = requests.post(
f"http://toing_url:{_e('TOING_PORT')}/toing",
json={
"m_type": self.m_type,
"models_list": self.model_names,
**self.payload_inputs
},
files={
'arr': compressed_arr_file
}
)
The FastAPI endpoint thazt accepts this request is as follows.
@APP.post("/toing")
async def invocations(
request: Request,
m_request_body: MRequestBody,
image_arr: UploadFile
):
image_arr_decoded = await image_arr.read()
...
from pydantic import BaseModel
class MRequestBody(BaseModel):
m_type: str
models_list: list
...
Python’s requests sets the request content type as multipart/form-data
. This gives me a 422 error. However when I remove the files part form the request and from the endpoint, the request is accepted by FastAPI. I noticed that when I remove the files param, the request is sent as application/json
.
Is there anyway I can send the file and the json in the same request and get my FastAPI’s pydantic model to accept the json data?