Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Request send image

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading