10

如何使用 Flask 处理长 GET 请求?是否有可能获得所有参数的字典?

这是我要解析的查询字符串:

/?msisdn=19150000001&to=12108054321&messageId=000000FFFB0356D1&text=This+is+an+inbound+message &type=text&message-timestamp=2012-08-19+20%3A38%3A23

这是我必须得到的代码,它只获得'msisdn'值:

import os
from flask import Flask
from flask import request
from flask import jsonify

app = Flask(__name__)

@app.route('/', methods=['GET'])
    def hello():
        if request.method == 'GET':
            json = request.args.getlist('msisdn')
            return str(json)
        else:
            return 'Hello World!'
4

2 回答 2

22

如果要转换查询字符串(MultiDict 到普通 dict)

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/")
def hello():
    all_args = request.args.to_dict()
    return jsonify(all_args)
于 2016-01-04T19:28:40.033 回答
11

是的。request.args是一个MultiDict

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/")
def hello():
    all_args = request.args.lists()
    return jsonify(all_args)
于 2014-06-18T18:45:16.423 回答