Getting the array as GET query parameters in Python

PythonListFlaskRequestWerkzeug

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRomeo M.View Question on Stackoverflow
Solution 1 - PythoncschornView Answer on Stackoverflow
Solution 2 - PythonRob CowieView Answer on Stackoverflow