57

I know in php I could just use $_GET['key1']['key2'] to retrieve GET data that is sent in the form of an array but is that something possible in Python as I just receive a string and it's not recognized as an array/list.

I use flask/werkzeug if that matters.

AlessioX
  • 3,167
  • 6
  • 24
  • 40
Romeo M.
  • 3,218
  • 8
  • 35
  • 40
  • 3
    Learn to read the question and what the people actually want before voting to close a question. I know how to get the request params, thats not what I asked about. Your url brings nothing new. – Romeo M. Oct 29 '11 at 16:44
  • 4
    I didn't vote to close anything. After all I have less than 100 rep, so I can't do that. I gave you a link that might be helpful. – Confluence Oct 29 '11 at 16:59

2 Answers2

74

The deep parsing of argument names is unique for PHP AFAIK.

If you need just a simple list, just pass several parameters with the same name and use request.args.getlist(<paramname>) (documentation).

Otherwise you have to parse the query string yourself.

cschorn
  • 1,191
  • 10
  • 11
68

request.args is a MultiDict instance (MultiDict, Flask request api).

request.args[key] ## returns a single value, the first if there are multiple
request.args.getlist(key) ## returns a list

If you want to submit structures more complex than can be encoded using simple key:vals, consider sending a json encoded object.

Also, look at the jQuery recursive param serialisation pattern, and the jquery-unparam lib which can deserialise it.

Rob Cowie
  • 22,259
  • 6
  • 62
  • 56
  • 7
    getlist would help if all vars are passed as `&var[]=1&var[]=2...`. In this case, I'd simply use `request.args.getlist('var[]')`. If params are passed with keys, however: `&var[0]=1&var[1]=2...` — it becomes an issue… – pilat Jan 04 '19 at 11:30
  • Hi...!!! I was using this and it wasn't working, I was using the brackets. Thanks a lot because it wasn't obvious and now is working fine :) – Daniel Azamar Nov 06 '20 at 03:26