1

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?

2
  • So is the question "How can I send a post using curl to my flask app" or "How can my flask app accept query parameters"? I'm a bit confused as to what you're looking for. Commented Feb 20, 2015 at 19:42
  • How can my flask app also accept query parameters". I am able to POST data using CURL. Commented Feb 20, 2015 at 19:47

2 Answers 2

2

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):
        ...
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.