Skip to content

Commit

Permalink
Fix issue #737
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Mar 17, 2019
1 parent 9fa733f commit 4ce074c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
7 changes: 2 additions & 5 deletions hug/input_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions hug/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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

Expand Down

0 comments on commit 4ce074c

Please sign in to comment.