0

I'm developing a Flask application in which i need to decode the query arguments of the url to work with them in my functions. I was wondering what is the best practice to convert a list-like field. Currently i encode the query before sending it to the server with urllib.urlencode() to create a json like string that gets decoded with this function:

def jsonToDict(param):

    try:
        jstring   = json.loads(json.dumps(param)) #this get me a json formatted string
        item      = literal_eval(jstring) # this converts it into a dict
    except:
        return None
    else:
        return item


field     = request.args.get('field')
variable  = jsonToDict(field)

that returns a dictionary. If i want to send a simple list of names : ['a','b','c'] i could use the above function with an url wrote like this:

http://mydomain/query?var=['a'%2C+'b'%2C+'c']

which works but looks rather clunky.

My question is: Is it advisable to send the query like this:

 http://mydomain/query?var=a,b,c

and if so, what's the proper way to create a tuple/list out of it ? With numbers (http://mydomain/query?var=1,2,3) i could easly use ast.literal_eval() to obtain the tuple i'm looking for, but with characters and strings it doesn't seems the way to go... any advice ?

darkpirate
  • 712
  • 3
  • 10
  • 27

0 Answers0