如何使用Flask处理GET查询字符串

时间:2014-06-18 18:35:10

标签: python rest flask nexmo

如何使用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!'

2 个答案:

答案 0 :(得分:13)

如果要将查询字符串(MultiDict转换为普通字典)

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/")
def hello():
    all_args = request.args.to_dict()
    return jsonify(all_args)

答案 1 :(得分:10)

是。 request.argsMultiDict

from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/")
def hello():
    all_args = request.args.lists()
    return jsonify(all_args)