Commit 942a67c0 authored by Mark Moissette's avatar Mark Moissette

added handling of empty request data (for post, put etc)

parent 82912d8b
...@@ -380,19 +380,20 @@ class RequestRouter: ...@@ -380,19 +380,20 @@ class RequestRouter:
'''Automatically parses JSON,XML,YAML if present''' '''Automatically parses JSON,XML,YAML if present'''
if request.method in (Http.POST,Http.PUT) and HttpHeader.CONTENT_TYPE in request.received_headers.keys(): if request.method in (Http.POST,Http.PUT) and HttpHeader.CONTENT_TYPE in request.received_headers.keys():
contentType = request.received_headers["content-type"] contentType = request.received_headers["content-type"]
data = request.content.read()
if contentType == MediaType.APPLICATION_JSON: if contentType == MediaType.APPLICATION_JSON:
try: try:
request.json = json.loads(request.content.read()) request.json = json.loads(data) if data else {}
except Exception as ex: except Exception as ex:
raise TypeError("Unable to parse JSON body: %s" % ex) raise TypeError("Unable to parse JSON body: %s" % ex)
elif contentType in (MediaType.APPLICATION_XML,MediaType.TEXT_XML): elif contentType in (MediaType.APPLICATION_XML,MediaType.TEXT_XML):
try: try:
request.xml = ElementTree.XML(request.content.read()) request.xml = ElementTree.XML(data)
except Exception as ex: except Exception as ex:
raise TypeError("Unable to parse XML body: %s" % ex) raise TypeError("Unable to parse XML body: %s" % ex)
elif contentType == MediaType.TEXT_YAML: elif contentType == MediaType.TEXT_YAML:
try: try:
request.yaml = yaml.safe_load(request.content.read()) request.yaml = yaml.safe_load(data)
except Exception as ex: except Exception as ex:
raise TypeError("Unable to parse YAML body: %s" % ex) raise TypeError("Unable to parse YAML body: %s" % ex)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment