I have a fastapi app where one of my endpoints reads as:
@app.post(
"/testing",
name="testing",
tags=["pluggin"],
)
def testing(images: List[bytes] = File(...)):
#do whatever
how can I send a request with an image so the endpoint will read it correctly? Right now I have:
import requests
f = io.BytesIO(file readed in binary)
files = {'file': f.getvalue()}
r = requests.post("mywebsite:myport/testing", files=files)
but it returns me response 422
>Solution :
Your requests payload must contains the same name as in the function parameter(images).
This means,
def testing(images: List[bytes] = File(...)):
^^^^^^^
and
files = {'file': f.getvalue()}
^^^^^^
must be same. So changing your code to following will work as expected.
import requests
f = io.BytesIO(file readed in binary)
files = {'images': f.getvalue()}
^^^^^
r = requests.post("mywebsite:myport/testing", files=files)