获取所有post或者get请求参数

转自: https://blog.csdn.net/u013055678/article/details/70214756


目录结构:

app

|----static(空)

|----templates(空)

|----flaskapp.py


flaskapp.py:

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2. #__author__="ZJL"    
  3.     
  4. from flask import Flask    
  5. from flask import request    
  6. from flask import make_response,Response    
  7.     
  8. import json    
  9.     
  10. app = Flask(__name__)    
  11.    
  12.    
  13. @app.route('/')    
  14. def hello_world():    
  15.     return 'hello world'    
  16.     
  17. def Response_headers(content):    
  18.     resp = Response(content)    
  19.     resp.headers['Access-Control-Allow-Origin'] = '*'    
  20.     return resp    
  21.    
  22. @app.route('/test', methods=['POST'])    
  23. def test():    
  24.     if request.method == 'POST' and request.form.get('aaa'):    
  25.         # POST:  
  26.         # request.form获得所有post参数放在一个类似dict类中,to_dict()是字典化  
  27.         # 单个参数可以通过request.form.to_dict().get("xxx","")获得  
  28.         # ----------------------------------------------------  
  29.         # GET:  
  30.         # request.args获得所有get参数放在一个类似dict类中,to_dict()是字典化  
  31.         # 单个参数可以通过request.args.to_dict().get('xxx',"")获得  
  32.         datax = request.form.to_dict()  
  33.         content = str(datax)    
  34.         resp = Response_headers(content)    
  35.         return resp    
  36.     else:    
  37.         content = json.dumps({"error_code":"1001"})    
  38.         resp = Response_headers(content)    
  39.         return resp    
  40.    
  41.    
  42. @app.errorhandler(403)    
  43. def page_not_found(error):    
  44.     content = json.dumps({"error_code""403"})    
  45.     resp = Response_headers(content)    
  46.     return resp    
  47.    
  48. @app.errorhandler(404)    
  49. def page_not_found(error):    
  50.     content = json.dumps({"error_code""404"})    
  51.     resp = Response_headers(content)    
  52.     return resp    
  53.    
  54. @app.errorhandler(400)    
  55. def page_not_found(error):    
  56.     content = json.dumps({"error_code""400"})    
  57.     # resp = Response(content)    
  58.     # resp.headers['Access-Control-Allow-Origin'] = '*'    
  59.     resp = Response_headers(content)    
  60.     return resp    
  61.     # return "error_code:400"    
  62.    
  63. @app.errorhandler(410)    
  64. def page_not_found(error):    
  65.     content = json.dumps({"error_code""410"})    
  66.     resp = Response_headers(content)    
  67.     return resp    
  68.    
  69. @app.errorhandler(500)    
  70. def page_not_found(error):    
  71.     content = json.dumps({"error_code""500"})    
  72.     resp = Response_headers(content)    
  73.     return resp    
  74.     
  75. if __name__ == '__main__':    
  76.     app.run(debug=True,threaded=True)   
  77.     

用request.form接收所有post参数



主要一次前端发请求过来的post参数个数不固定,如果用request.form['formname']这种形式会因为接收不到参数报500的错误




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值