From 4ce074cedf57ecb26491c2b4d5e12872cda26094 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sun, 17 Mar 2019 14:21:39 -0700 Subject: [PATCH] Fix issue #737 --- hug/input_format.py | 7 ++----- hug/interface.py | 3 +-- tests/test_decorators.py | 4 ++-- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/hug/input_format.py b/hug/input_format.py index 79d3a695..cbbdcf17 100644 --- a/hug/input_format.py +++ b/hug/input_format.py @@ -68,16 +68,13 @@ def urlencoded(body, charset='ascii', **kwargs): @content_type('multipart/form-data') -def multipart(body, **header_params): +def multipart(body, content_length=0, **header_params): """Converts multipart form data into native Python objects""" + header_params.setdefault('CONTENT-LENGTH', content_length) if header_params and 'boundary' in header_params: if type(header_params['boundary']) is str: header_params['boundary'] = header_params['boundary'].encode() - body_content_length = getattr(body, 'content_length', None) - if body_content_length: - header_params['CONTENT-LENGTH'] = body_content_length - form = parse_multipart((body.stream if hasattr(body, 'stream') else body), header_params) for key, value in form.items(): if type(value) is list and len(value) is 1: diff --git a/hug/interface.py b/hug/interface.py index 9f0c3e7e..f4f8ede2 100644 --- a/hug/interface.py +++ b/hug/interface.py @@ -604,11 +604,10 @@ def gather_parameters(self, request, response, context, api_version=None, **inpu if self.parse_body and request.content_length: body = request.stream - body.content_length = request.content_length content_type, content_params = parse_content_type(request.content_type) body_formatter = body and self.inputs.get(content_type, self.api.http.input_format(content_type)) if body_formatter: - body = body_formatter(body, **content_params) + body = body_formatter(body, content_length=request.content_length, **content_params) if 'body' in self.all_parameters: input_parameters['body'] = body if isinstance(body, dict): diff --git a/tests/test_decorators.py b/tests/test_decorators.py index 0c94a089..dae2f6f0 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -662,7 +662,7 @@ def my_method(): def test_input_format(): """Test to ensure it's possible to quickly change the default hug output format""" old_format = api.http.input_format('application/json') - api.http.set_input_format('application/json', lambda a: {'no': 'relation'}) + api.http.set_input_format('application/json', lambda a, **headers: {'no': 'relation'}) @hug.get() def hello(body): @@ -682,7 +682,7 @@ def hello2(body): @pytest.mark.skipif(sys.platform == 'win32', reason='Currently failing on Windows build') def test_specific_input_format(): """Test to ensure the input formatter can be specified""" - @hug.get(inputs={'application/json': lambda a: 'formatted'}) + @hug.get(inputs={'application/json': lambda a, **headers: 'formatted'}) def hello(body): return body