From e70b0e425bd81107a9495be8a089ae08097259f8 Mon Sep 17 00:00:00 2001 From: Miikka Koskinen Date: Thu, 15 May 2014 20:02:38 +0300 Subject: [PATCH] Allow /get with multiple values for the same key /get now returns a list of values if there were more than one value for the same key in the query sting. This mirrors the behavior of /post with form fields. Fixes #134. --- httpbin/helpers.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/httpbin/helpers.py b/httpbin/helpers.py index 6fc6281..c337791 100644 --- a/httpbin/helpers.py +++ b/httpbin/helpers.py @@ -110,6 +110,20 @@ def get_headers(hide_env=True): return CaseInsensitiveDict(headers.items()) +def semiflatten(multi): + """Convert a MutiDict into a regular dict. If there are more than one value + for a key, the result will have a list of values for the key. Otherwise it + will have the plain value.""" + if multi: + result = multi.to_dict(flat=False) + for k, v in result.items(): + if len(v) == 1: + result[k] = v[0] + return result + else: + return multi + + def get_dict(*keys, **extras): """Returns request dict of given keys.""" @@ -125,22 +139,16 @@ def get_dict(*keys, **extras): data = form.keys().pop() form = None - if form: - nonflat_dict = form.to_dict(flat=False) - for k, v in nonflat_dict.items(): - if len(v) == 1: - nonflat_dict[k] = v[0] - form = nonflat_dict + form = semiflatten(form) try: _json = json.loads(data) except ValueError: _json = None - d = dict( url=request.url, - args=request.args, + args=semiflatten(request.args), form=form, data=json_safe(data), origin=request.headers.get('X-Forwarded-For', request.remote_addr),