Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of parameters in flask: reading only first value getlist()

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
like image 449
user305883 Avatar asked Feb 10 '17 23:02

user305883


People also ask

How do I get URL parameters in Flask?

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.

How do you process incoming request data in Flask?

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.

How do you handle a post request in Flask?

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.


1 Answers

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.

like image 110
MrLeeh Avatar answered Oct 12 '22 19:10

MrLeeh