flask之urlencode参数传递和解析
app.py
# -*- coding: utf-8 -*-
from flask import Flask, request, jsonify, Response
import json
from fun import fun
def response_headers(content):
resp = Response(content)
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
@app.route('/rq/', methods=['POST'])
def response():
userId = request.args.get('userId', 1, type=int) # 获取指定参数
# 1:userId的默认值,没有时默认None
# type:类型,默认str
page = request.args.get('page', 1, type=int) # 获取指定参数
limit = request.args.get('limit', 1, type=int) # 获取指定参数
print(userId,page,limit)
try:
result = fun(userId,page,limit
out = {
"result": result,
"status": 0
}
return jsonify(out)
except:
out = {
"result": [],
"status": 9
}
content = json.dumps(out)
resp = response_headers(content)
return resp
if __name__ == '__main__':
app.run(host='localhost',port=2222,threaded=True,debug = False)
fun.py
def fun(userId,page,limit):
return userId+page+limit
请求1:
http://localhost:2222/rq/?userId=1&page=1&limit=100
请求2:
http://localhost:2222/rq