一般的代理方式' GET',' POST'和'删除'在烧瓶中

时间:2017-01-19 11:07:45

标签: python flask

目前,我们将该呼叫代理到另一项服务。为此,我们检查request.methodGET,我们使用request.get(url),如果request.methodPOST,我们使用request.post(url)。是否有更清洁的实现来处理这种用法?

@app.route('/<path:url>', methods=('GET', 'POST'))
def foo(url):
    if check_token():
        req_url = "http://localhost:8081/{}?{}".format(url, request.query_string.decode('utf-8'))
        if request.method == 'GET':
            req = requests.get(req_url, stream=True)
        elif request.method == 'POST':
            req = requests.post(req_url, stream=True)

1 个答案:

答案 0 :(得分:0)

您可以使用两种不同的方法实现这两个调用。每种方法都应该只允许一种方法。

Array
    (
        [0] => Array
            (
                [id] => 1
                [code] => asq12d134
                [mail] => info@localhost
                [date] => 2017-01-18
            )
        [1] => Array
            (
                [id] => 2
                [code] => 1qeacetq
                [mail] =>info@localhost
                [date] => 2017-01-18
            )
                [2] => Array
            (
                [id] => 3
                [code] => awq12sq
                [mail] =>info@localhost
                [date] => 2017-01-18
            )
    )