Flask获取传参的方式
第一种方法:
配置:
from flask import Flask
from flask import request
from flask import jsonify
from flask_cors import CORS
import json
app = Flask(__name__)
CORS(app)
@app.route('/service/<servicename>&<env>')
def loader(servicename, env):
return servicename+"|"+env
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
调用:
http://127.0.0.1:8080/service/orders&prod
第二种方法:
配置:
from flask import Flask
from flask import request
from flask import jsonify
from flask_cors import CORS
import json
app = Flask(__name__)
CORS(app)
@app.route('/service', methods=['get'])
def loader():
servicename = request.values.get("servicename")
env = request.values.get("env")
return servicename+"|"+env
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
调用:
http://127.0.0.1:8080/service?servicename=order&env=sit
第三种方法:
配置:
from flask import Flask
from flask import request
from flask import jsonify
from flask_cors import CORS
import json
app = Flask(__name__)
CORS(app)
@app.route('/service', methods=["POST"])
def loader():
data = request.get_data()
new_data = json.loads(str(data, encoding="utf-8"))
return jsonify(new_data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
调用:
curl -X POST -d '{"servicename": "orders","env": "prod"}' http://10.10.30.135:8080/service