python flask通过post和url发送数据 [英] python flask send data through post and url

查看:41
本文介绍了python flask通过post和url发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于通过 CURL 通过 POST 发送数据和通过 URL 发送数据的问题.更一般地说,我有一个烧瓶路线

I have a question regarding sending data via a CURL through POST and sending data through the URL. More generally, I have a flask route

@app.route('/create', methods=['POST'])
def clone:
   ...

如何使用 URL 发送数据?我想做这样的事情:

How can I also send data using a URL? I want to do something like this:

<my-server>:port/create/arg1/arg2/arg3

我刚刚发现你可以做类似的事情

I just figured out you can do something like

@app.route('/create', methods=['POST'])
@app.route('/create/<op>/<source>/<target>', methods=['GET'])    
def clone(op = None, source = None, target = None):
    ...

哪个行得通.这是一个好方法吗?

Which will work. Is this a good approach?

推荐答案

要访问实际的查询字符串(? 之后的所有内容),请使用:

To access the actual query string (everything after the ?) use:

from flask import request

@app.route('/create', methods=['POST'])
    def clone:
        return request.query_string

访问已知查询参数:

from flask import request

@app.route('/create', methods=['POST'])
    def clone:
        user = request.args.get('user')

如果您想使用像 /arg1/arg2/arg3 一样发送的变量,请使用:

If you're looking to use variables sent like /arg1/arg2/arg3 use:

@app.route('/create/<arg1>/<arg2>/<arg3>', methods=['POST'])
    def clone(arg1, arg2, arg3):
        ...

这篇关于python flask通过post和url发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆