I would like to pass a list as parameter to flask via getlist(). Reading here: REST API Best practice: How to accept list of parameter values as input
Could you help figuring out why this simple code failed to read a list parameter (skip_id) ?
def api_insight(path):
import pdb
skip_id = request.args.getlist('skip_id', type=None)
print( 'skip_id', skip_id)
pdb.set_trace()
curl http://myexample.com/<mypath>/?&skip_id=ENSG00000100030,ENSG00000112062
# empty list
curl http://myexample.com/<mypath>/?&skip_id=[ENSG00000100030,ENSG00000112062]
# empty list
curl http://myexample.com/<mypath>/?&skip_id=ENSG00000100030&skip_id=ENSG00000
# only first value is read in list
The URL parameters are available in request. args , which is an ImmutableMultiDict that has a get method, with optional parameters for default value ( default ) and type ( type ) - which is a callable that converts the input value to the desired format.
To access the incoming data in Flask, you have to use the request object. The request object holds all incoming data from the request, which includes the mimetype, referrer, IP address, raw data, HTTP method, and headers, among other things.
In order to demonstrate the use of POST method in URL routing, first let us create an HTML form and use the POST method to send form data to a URL. Now enter the following script in Python shell. After the development server starts running, open login. html in the browser, enter name in the text field and click Submit.
The last way should actually work. I just tried it in my Browser.
http://localhost:5000/api?skip_id=ENSG000001000301&skip_id=ENSG00000
Gives me
['ENSG00000100030', 'ENSG00000']
However with curl you will get into trouble with the &
character as it will put the task in the background (at least if you're on Linux). With curl you can use
curl -X GET -G http://localhost:5000/api?skip_id -d skip_id=ENSG00000100030 -d skip_id=ENSG00000
to get your described result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With