waka-waka-waka
waka-waka-waka

Reputation: 1045

python flask send data through post and 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:
   ...

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?

Upvotes: 1

Views: 8455

Answers (2)

Alejo Bernardin
Alejo Bernardin

Reputation: 1215

In case that you are looking for how to POST directly to your flask app, here is a way to make it in python

import request
 
dictionary = {"something":"100"}
response   = requests.post('http://127.0.0.1:5000/create', json=dictionary)
response.json()

you can also change .json() for .content or .text functions.

Upvotes: 0

Daniel Timberlake
Daniel Timberlake

Reputation: 1199

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

Upvotes: 2

Related Questions