使用 flask2postman 从 Flask 应用程序创建 Postman 集合
先决条件: Postman 简介,第一个使用 Flask 的应用程序
由于 Postman 在开发领域越来越受欢迎,本文解释了一种可以使用Python编写的命令行实用程序轻松将其与 Flask API 集成的方法。我们将使用flask2postman模块。但你会是为什么这个模块。这就是为什么——
- 它是从 Flask API 生成 Postman 集合的简单工具。
- 它适用于命令行。
- 提供了各种自定义,例如可配置的基本 URL 等。
- 输出是一个 JSON 文件,可以很容易地导入邮递员。
安装
要安装此类型,请在终端中输入以下命令。
pip install flask2postman
命令
flask2postman [-h] [-n NAME] [-b BASE_URL] [-a] [-i] [-f] flask_instance
参数:
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: Generat static files in folder /static/{{filename}}.
- -i, –indent: Intends the output in created .json() file.
- -f, –folders: If blueprints provided, adds subfolders for it.
分步实施
第 1 步:导入库并初始化应用上下文
Python3
from flask import Flask, render_template
app = Flask(__name__)
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/')
def gfg(page):
return render_template('gfg.html', page=page)
Python3
if __name__ == '__main__':
app.run()
第 2 步:添加 API 路由
蟒蛇3
# 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/')
def gfg(page):
return render_template('gfg.html', page=page)
第 3 步:运行应用
蟒蛇3
if __name__ == '__main__':
app.run()
在职的
在命令行上运行命令。
flask2postman flask-postman.app > gfg_postman.json
创建一个名为: gfg_postman.json的 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”: []}
下一步是将集合导入邮递员。
输出:
请注意附加的默认 {{base_url}} 和路径参数。
例子
flask2postman flask-postman.app –name “GFG Flask Collection” –base_url 127.0.0.1:5000 –i > custom_gfg_postman.json
custom_gfg_postman.json(现在缩进)
{
"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
}
输出 :
请注意在 JSON 集合中添加的集合名称、基本 URL 和缩进。