How To Send Images To Python FastAPI Using Postman

Hello Learners…

Welcome to the blog…

Topic: How To Send Images To Python FastAPI Using Postman

Table Of Contents

  • Introduction
  • How To Send Images To Python FastAPI Using Postman
  • Summary
  • References

Introduction

In this, we discuss How To Send Images To Python FastAPI Using Postman. we will explore how to utilize Postman, a popular API development tool, to send images to a FastAPI

Sending images to a Python FastAPI application is a common requirement when building image-processing or computer vision projects. Following the steps outlined below, we can seamlessly integrate image uploads into our FastAPI workflow.

To know about What FastAPI And Postman read our below articles,

How To Send Images To Python FastAPI Using Postman

So I hope you read our other articles related to FastAPI and Postman, If you don’t then don’t worry.

Requirements,

  • Python installed on the system
  • Postman installed on the system

Now we learn or implement different methods for sending images to Python FastAPI through the Postman tool.

First, install the required libraries,

pip install fastapi

pip install python-multipart

pip install "uvicorn[standard]"

These are normally basic libraries that are required when we are working with files in Python FastAPI.

Send Images To Python FastAPI By Uploading From Postman

First, we have to create a FastAPI using Python. this code accepts jpg, jpeg, and png files only.

So for that create a file named send_images.py and paste the below code into this file.

from fastapi import FastAPI,UploadFile, File
import uvicorn
import os 
app = FastAPI()

@app.get("/")
def hello():
    return {"API":"API is working fine"}

@app.post("/upload_image")
async def upload_image(img_file:UploadFile =File(...)):

    if '.jpg' in img_file.filename or '.jpeg' in img_file.filename or '.png' in img_file.filename:
        file_save_path="./images/"+img_file.filename
        if os.path.exists("./images") == False:
            os.makedirs("./images")

        with open(file_save_path, "wb") as f:
            f.write(img_file.file.read())

        if os.path.exists(file_save_path):
            return {"image_path":file_save_path,"message": "Image saved successfully"}
        else:
            return {"error":"Image Not saved !!!"}
    else:
        return {"error": "File Type is not valid please upload only jpg,jpeg and png"}


if __name__=="__main__":
    uvicorn.run(app)

Here we write code in such a way that first, it will check the proper image extension and after that, it will create a separate folder for storing the images, and if image files are not stored properly on the given path then it will return an error.

Now we run the code,

python3 send_images.py
send image to fastapi python using postman

Here in the terminal, we get an URL http://127.0.0.1:8000

So our full request URL is http://127.0.0.1:8000/upload_image

Now open the Postman add request and paste the above URL with the POST method.

In Postman select -> Body -> form-data and In the Key parameter enter the key name img_file which we use in our code and select that key as a File.

After that, we can see the Select Files button.

send image to fastapi python using postman

Now click on Select Files and select your files and Send the request.

send image to fastapi python using postman

Here we select the galaxyofai.jpg image file and send the request to the FastAPI and we get the response as below.

send image to fastapi python using postman

Also, we can see our image file in the images folder,

send image to fastapi python using postman

You can customize this code to suit your specific requirements, such as adding additional validation, processing, or saving logic.

Summary

So this is the one way using that we can send our images files to the FastAPI and use that images for the further process. there are many other methods too that we will see in upcoming articles.

Happy Learning And Keep Learning…

Thank You…

References

Leave a Comment