python flask 接受请求的get和post数据

今天使用flask的request来处理接受的get和post请求,刚刚接触,不太熟悉,导致一个小问题卡了很久,在这里记下来

request 接受get请求

使用request.args.get(“参数名称”)

@api.route('/special/list', methods=['GET'])
@auth.login_required
def get_special_pager_list():
    g.header = request.headers
    # print(g.header.get('Authorization'))
    if request.args is None:
        return Tools.generate_cors_response({'error': 1, 'code': 200, 'msg': '缺少必要参数'})

    config = current_app.config
    current = page = int(1)
    size = int(10)
    if request.args.get('current') is not None:
        current = int(request.args.get('current'))
    if request.args.get('page') is not None:
        page = int(request.args.get('page'))
    if request.args.get('size') is not None:
        size = int(request.args.get('size'))
    ......    

request 接收post,put请求

使用request.get_json()接收传递过来的所有数据
根据传的json的值,返回的结果可以使字典类型,也可能是列表
传入的json的值是key:value,如{“key”:“value”},则返回的结果是字典类型
传入的json的值是[“value1”,“value2”,“value3”],则返回的结果是列表类型

    # 字典类型的
    if request.method == 'POST' or request.method == 'PUT':
        get_json = request.get_json()
        special = Special.query.get({'special_id': special_id})
        if special is None:
            return Tools.generate_cors_response({'error': 1, 'code': 200, 'msg': '无相关专题信息'})
        if get_json['style_code'] is not None:
            special.style_code = get_json['style_code']
        if get_json['js_code'] is not None:
            special.js_code = get_json['js_code']
        if get_json['wap_style_code'] is not None:
            special.wap_style_code = get_json['wap_style_code']
        if get_json['wap_js_code'] is not None:
            special.wap_js_code = get_json['wap_js_code']
        

你可能感兴趣的:(python,flask,python,flask)