python flask send data through post and url

2019-08-13 23:23发布

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:
   ...

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?

1条回答
仙女界的扛把子
2楼-- · 2019-08-13 23:44

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

To access known query parameters:

from flask import request

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

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):
        ...
查看更多
登录 后发表回答