Open In App

Create Postman collection from Flask application using flask2postman

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Introduction to Postman, First App using Flask

Since Postman is gaining popularity in the development domain, this article explains a way in which it can be easily integrated with Flask APIs using command-line utility written in Python. We will be using the flask2postman module. But you will be why this module. Here’s why – 

  • It is a straightforward tool to generate Postman collection from Flask APIs.
  • It works in Command-Line.
  • Various customizations provided such as configurable base URLs, etc.
  • Output is a JSON file, which can be easily imported to the postman.

Installation

To install this type the below command in the terminal.

pip install flask2postman

Command 

flask2postman [-h] [-n NAME] [-b BASE_URL] [-a] [-i] [-f] flask_instance

Parameters:

Positional arguments: flask_instance

Optional arguments:

  • -h, –help: Prints help and exits
  • -n NAME, –name NAME: Name of postman Collection. Defaults to name of app/current directory
  • -b BASE_URL, –base_url BASE_URL: Base url ( Server ) of all APIs in Postman Collection. Defaults to {{base_url}}.
  • -a, –all: If provided generations OPTION/HEAD methods.
  • -s, –static: Generate static files in folder /static/{{filename}}.
  • -i, –indent: Intends the output in created .json() file.
  • -f, –folders: If blueprints provided, adds subfolders for it.

Stepwise implementation

Step 1: Importing libraries and initializing app context

Python3




from flask import Flask, render_template
 
app = Flask(__name__)


Step 2: Adding API routes

Python3




# GET API
@app.route('/')
def index():
    return render_template('index.html')
 
 
# POST API
@app.route('/add', methods = ['POST'])
def post_data():
    return render_template('form.html')
 
 
# GET API with path param
@app.route('/gfg/<int:page>')
def gfg(page):
    return render_template('gfg.html', page=page)


Step 3: Running app

Python3




if __name__ == '__main__':
    app.run()


Working 

Run the command on the command line.

flask2postman flask-postman.app > gfg_postman.json

Creates a JSON file with the name: gfg_postman.json

{“id”: “516d259f-77b8-4aa9-9f13-59feb59d0cb4”, “name”: “gfg”, “timestamp”: 1621215826922, “requests”: [{“id”: “95f002e8-9923-4123-b7a8-dd39a38c6e20”, “data”: [], “description”: “”, “headers”: “”, “method”: “GET”, “name”: “gfg”, “time”: 1621215826922, “url”: “{{base_url}}/gfg/{{page}}”, “collectionId”: “516d259f-77b8-4aa9-9f13-59feb59d0cb4”, “dataMode”: “params”}, {“id”: “0ca2ca2f-33b6-4a37-936e-bba48a5592fa”, “data”: [], “description”: “”, “headers”: “”, “method”: “GET”, “name”: “index”, “time”: 1621215826922, “url”: “{{base_url}}/”, “collectionId”: “516d259f-77b8-4aa9-9f13-59feb59d0cb4”, “dataMode”: “params”}, {“id”: “234d7de4-d1c8-4d14-b86b-d2c71e338b54”, “data”: [], “description”: “”, “headers”: “”, “method”: “POST”, “name”: “data”, “time”: 1621215826922, “url”: “{{base_url}}/add”, “collectionId”: “516d259f-77b8-4aa9-9f13-59feb59d0cb4”, “dataMode”: “params”}], “order”: [“95f002e8-9923-4123-b7a8-dd39a38c6e20”, “0ca2ca2f-33b6-4a37-936e-bba48a5592fa”, “234d7de4-d1c8-4d14-b86b-d2c71e338b54”], “folders”: []}

The next step is to import the collection into postman. 

Output:

Imported Postman Collection

Notice the default {{base_url}} and path parameter appended. 

Example

flask2postman flask-postman.app  –name “GFG Flask Collection” –base_url 127.0.0.1:5000 –i > custom_gfg_postman.json

custom_gfg_postman.json ( Indented now )

{
    "folders": [],
    "id": "e76915b6-2051-445c-934d-625059e82ed1",
    "name": "GFG Flask Collection",
    "order": [
        "7c303b3b-d9cc-4c87-80f0-2e6ddbd8ec07",
        "a08769de-09ee-49e1-b528-08d38293fe48",
        "3259b993-364b-421c-adc1-df3f57ea9048"
    ],
    "requests": [
        {
            "collectionId": "e76915b6-2051-445c-934d-625059e82ed1",
            "data": [],
            "dataMode": "params",
            "description": "",
            "headers": "",
            "id": "7c303b3b-d9cc-4c87-80f0-2e6ddbd8ec07",
            "method": "GET",
            "name": "gfg",
            "time": 1621216574211,
            "url": "127.0.0.1:5000/gfg/{{page}}"
        },
        {
            "collectionId": "e76915b6-2051-445c-934d-625059e82ed1",
            "data": [],
            "dataMode": "params",
            "description": "",
            "headers": "",
            "id": "a08769de-09ee-49e1-b528-08d38293fe48",
            "method": "GET",
            "name": "index",
            "time": 1621216574211,
            "url": "127.0.0.1:5000/"
        },
        {
            "collectionId": "e76915b6-2051-445c-934d-625059e82ed1",
            "data": [],
            "dataMode": "params",
            "description": "",
            "headers": "",
            "id": "3259b993-364b-421c-adc1-df3f57ea9048",
            "method": "POST",
            "name": "data",
            "time": 1621216574211,
            "url": "127.0.0.1:5000/add"
        }
    ],
    "timestamp": 1621216574211
}

Output :

Customized Postman Collection

Notice the collection name, base URL, and indentations being added in JSON collection. 



Last Updated : 28 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads