zoukankan      html  css  js  c++  java
  • Flask获取数据的一些方法

    前提:基于纯后端服务,

    post 请求 (Content-Type: application/json,)

    1.获取未经处理过的原始数据而不管内容类型,如果数据格式是json的,则取得的是json字符串,排序和请求参数一致

    c = request.get_data()    

    2.将请求参数做了处理,得到的是字典格式的,因此排序会打乱依据字典排序规则

    c =request.get_json()

    3.可以获取未经处理过的原始数据,如果数据格式是json的,则取得的是json字符串,排序和请求参数一致

    c = request.data

    4.将请求参数做了处理,得到的是字典格式的,因此排序会打乱依据字典排序规则

    c = request.json

    ps: 刚开始使用的时候以为是一个方法这样调用request.json()然后报错如下:

         Content-Type: application/json时报错'dict' object is not callable

         原来是个属性,因此这样使用request.json,就能正常使用了

    我个人做flask取post请求参数一般都是这样用:

    a = request.json['a']

    get请求 (Content-Type: application/json,)  

    request.args.get('key')  #可以获取到单个的值,
    

    requestValues = request.args #可以获取get请求的所有参数返回值是ImmutableMultiDict类型,

    requestValues.to_dict() #将获得的参数转换为字典

    我个人做flask取get请求参数一般都是这样用:

    a = request.args.get('a')
    复制代码
    #请求头信息
    request.headers 
    #请求方法
    request.method 
    #请求url   
    request.url
    

    from flask import Flask from flask_restful import Api,Resource

    app = Flask(name)
    api = Api(app)

    class HelloWorld(Resource):
    def get(self):
    return {'hello': 'world'}

    api.add_resource(HelloWorld, '/')

    if name == 'main':
    app.run(debug=True)

    通过api.add_resource

  • 相关阅读:
    leetcode_357. Count Numbers with Unique Digits
    leetcode_712. Minimum ASCII Delete Sum for Two Strings
    leetcode_865. Smallest Subtree with all the Deepest Nodes
    leetcode_919. Complete Binary Tree Inserter
    leetcode_1014. Capacity To Ship Packages Within D Days
    leetcode_998. Maximum Binary Tree II
    leetcode_654. Maximum Binary Tree
    leetcode_894. All Possible Full Binary Trees
    leetcode_486. Predict the Winner
    二叉树的遍历
  • 原文地址:https://www.cnblogs.com/huanghongzheng/p/14866525.html
Copyright © 2011-2022 走看看