From c791984de4db5f661799ea926b4e174c3f6063c2 Mon Sep 17 00:00:00 2001 From: Jonathan Camp Date: Thu, 18 Oct 2012 10:32:45 +0200 Subject: [PATCH] correctly handle empty POST parameters --- tornado/httputil.py | 3 +-- tornado/test/httpserver_test.py | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tornado/httputil.py b/tornado/httputil.py index 0f8a8438..26b33cd0 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -215,9 +215,8 @@ def parse_body_arguments(content_type, body, arguments, files): that will be updated with the parsed contents. """ if content_type.startswith("application/x-www-form-urlencoded"): - uri_arguments = parse_qs_bytes(native_str(body)) + uri_arguments = parse_qs_bytes(native_str(body), keep_blank_values=True) for name, values in uri_arguments.iteritems(): - values = [v for v in values if v] if values: arguments.setdefault(name, []).extend(values) elif content_type.startswith("multipart/form-data"): diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 190f1abc..7ee82b83 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -247,6 +247,8 @@ class EchoHandler(RequestHandler): def get(self): self.write(recursive_unicode(self.request.arguments)) + def post(self): + self.write(recursive_unicode(self.request.arguments)) class TypeCheckHandler(RequestHandler): def prepare(self): @@ -300,6 +302,11 @@ class HTTPServerTest(AsyncHTTPTestCase, LogTrapTestCase): data = json_decode(response.body) self.assertEqual(data, {u"foo": [u"\u00e9"]}) + def test_empty_post_parameters(self): + response = self.fetch("/echo", method="POST", body="foo=&bar=") + data = json_decode(response.body) + self.assertEqual(data, {u"foo": [u""], u"bar": [u""]}) + def test_types(self): headers = {"Cookie": "foo=bar"} response = self.fetch("/typecheck?foo=bar", headers=headers)