How To Send Image Files To Flask API Using Postman?

Hello Learners…

Welcome to my blog…

Table Of Contents:

  • Introduction
  • Send image files to flask API using postman
  • Send jpg or jpeg files to flask API
  • Summary

Introduction

In this post, we discuss how we can send image files to Flask API using Postman through post request and save that image file into our current directory and use it for further processing.

Send Image Files To Flask API Using Postman

This can be really helpful when you’re working on projects that involve uploading images to your web applications. Let’s get started:

1. Set Up Your Flask API
  • First things first, make sure we have our Flask API up and running. We should have an endpoint in our API that’s designed to receive image files.
2. Install and Open Postman
  • If you haven’t done so already, go ahead and download and install Postman on your computer. Once it’s installed, open the application.
3. Create a New Request
  • In Postman, click on the “New” button and select “Request” to create a new API request.
4. Choose the HTTP Method
  • Choose the appropriate HTTP method for uploading files, which is usually “POST” in this case.
5. Enter the API URL
  • Enter the URL of our Flask API endpoint that’s meant to handle the image file uploads.
6. Set Up Headers (If Needed)
  • If our API requires specific headers like “Content-Type” or “Authorization,” go ahead and add those in the appropriate section.
7. Go to the Body Tab
  • Navigate to the “Body” tab within our Postman request.
8. Select the Form-Data Option
  • In the “Body” section, choose the “form-data” option.
9. Add a Key-Value Pair
  • Add a key-value pair where the key should match the parameter name our Flask API expects for the image file. The value will be the image file itself.
10. Choose the File Type for the Value
  • For the value of the key-value pair, change the type to “File” using the dropdown menu next to the value field. This will let us select a file from our computer.
11. Choose and Upload the File
  • Click on the “Choose Files” button and pick the image file we want to upload.
12. Send the Request
  • With everything set up, go ahead and click the “Send” button to initiate the request to our Flask API.
13. Review the Response
  • We should receive a response from our Flask API, which we can review within Postman.

Send JPG Or JPEG Files To Flask API

Here we learn how to send our jpg or jpeg file to our flask API and save it into our current working directory.

First, we have to create a flask API using python for that first we install the python flask library using the pip command.

Open your terminal and enter the below command.

pip install flask

after the successful installation of the flask library, we create a flask API using python code.

Create an app.py file and paste the below code into that file

here is the flask API code:

from flask import Flask,request

app=Flask(__name__)

@app.route("/",methods=["POST","GET"])
def main():
    try:
        if request.method=='POST':
            # print("request",request.form)
            image=request.files['image']
            image_name=image.filename
            if '.jpg' in image_name:
            
                
                image.save(image_name)

                return {"response":"file saved successfully in your current durectory"}
            elif '.jpeg' in image_name: 
                image.save(image_name)

                return {"response":"file saved successfully in your current durectory"}
            else:
                return {"error":"select you image file"}
    except Exception as e:
        return {"error":str(e)}

    

if __name__=="__main__":
    app.run("0.0.0.0",debug=True)

Now we have to run the app.py file, to run the file open the terminal and run the bellow command.

python3 app.py

If in the code everything is fine then you get an URL: http://127.0.0.1:5000

Now open the postman, and use this URL as a POST request as you can see below.

screenshot by the author

After that select Body > form-data.

Enter the key name Image and as you can see here we use the file.

screenshot by the author

After that select Value which is the image file that we want to send to flask API.

screenshot by the author

Here we use a jpg file or we can use a jpeg also, now click the send button.

screenshot by the author

Here we can see the response of our flask API and we can find our 4.jpg file in our current working directory.

Summary

This is a simple method of many other methods we can use to send our images to flask API using postman and after that, we can use that image for further processing.

Also, read other articles related to this.

Send Videos to Flask API Using Postman

References:

2 thoughts on “How To Send Image Files To Flask API Using Postman?”

Leave a Comment