Nested GET parameters for a HTTP API

You’ll have to name each parameter explicitly:

data = {'q.field':'project_id', 'q.value': 's56464dsf6343466'}

There is no standard for marshalling more complex structures into GET query parameters; the only standard that exists gives you key-value pairs. As such, the requests library doesn’t support anything else.

You can of course write your own marshaller, one that takes nested dictionaries or lists and generates a flat dictionary or tuple of key-value pairs for you.

You have to encode the data first, like this:

from requests import get
from urllib import urlencode

url="http://localhost:8777/v2/meters/instance"
data = {'q': [{'field':'project_id', 'value': 's56464dsf6343466'}]}
get(url, params=urlencode(data), headers=HDR)

And then you decode it like this:

from ast import literal_eval

@route('/v2/meters/instance')
def meters():
    kwargs = request.args.to_dict()
    # kwargs is {'q': "[{'field':'project_id', 'value': 's56464dsf6343466'}]"}
    kwargs = {k: literal_eval(v) for k, v in kwargs.iteritems()}
    # kwargs is now {'q': [{'field':'project_id', 'value': 's56464dsf6343466'}]}

Read more here: Source link