Send Text Data To Flask API Using Postman?

Hello Learners…

Welcome to my blog…

Table Of Content:

  • Introduction
  • What is a Flask?
  • What is a Postman?
  • Send Text Data To Flask API Using Postman
  • Send text data as a JSON to Flask API using Postman
  • Send text data as a form to Flask API using Postman
  • Summary

Introduction:

In this post, We implement some methods to send our text data to Flask API from the postman. using this method we can send our text data to Flask API and see the response of our Flask API.it is used to test our Flask API using Postman before our API goes on the production server.it is good practice to try our API using Postman.

What is a Flask?

Flask is easy to get started with and can be used to build a wide range of web applications, from simple to complex, production-ready ones.

What is a Postman?

Postman is an application used for API testing. An HTTP client tests HTTP requests, utilizing a graphical user interface, through which we obtain different types of responses that need to be subsequently validated.

Send Text Data To Flask API Using Postman

We can send our text data using two methods:

  1. As a JSON file
  2. As a Form

Let’s discuss both methods…

Send text data as a JSON to Flask API using Postman

First, we have to create a Flask API. For this first, we need to install a flask using pip.

pip install flask

Here is the Python code for the create a Flask API.

Create a file app.py and add the below code into that file.


from flask import Flask,request

app=Flask(__name__)

@app.route("/",methods=["POST","GET"])
def main():
    try:
        if request.method=='POST':
       
            try:
                text = request.json['text']
                print(text)
                return {"your text":text}

            except Exception as e:
                return {"error":str(e)}

    except Exception as e:
        return {"error":str(e)}


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

Run the app.py file using the command:

python3 app.py

Weget an URL http://localhost:5000/

Now open our postman and send a POST request as below.

JSON Format

{
       "text" : "artificial intelligence"
}

Note: Select raw and JSON format as you can see here.

{
"your text": "artificial intelligence"
}

It is the response of our Flask API.

Continue…….

Summary

This is the simple method to send our text data to Flask API using Postman.

Happy Learning And Keep Learning…

Thank You…

References:

1 thought on “Send Text Data To Flask API Using Postman?”

Leave a Comment