[Kimchi-devel] [PATCH] bug fix: return empty dict when the request body is empty

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Wed Feb 12 06:30:50 UTC 2014


From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>

in test mode, you can user curl or HttpRequester to try as follow.
$ curl -u <user> -H 'Accept: application/json' -H 'Content-type:
application/json'  http://localhost:8000/templates/test-template-4 -X
PUT -d ''

kimchi will throw HTTPError: (400, 'Unable to parse JSON request')

Now remove the check Content-Length in request headers.

No Content-Length in request headers means this request without body.
GET and DELETE methods allow a request without body.
But PUT and POST methods require body in request.

After remove this check, new check for the length of body.

And also now we have decide not to pass parameters by body for GET method.
That means GET will not call parse_request.

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
 src/kimchi/control/utils.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py
index 9c6878b..21a6ce8 100644
--- a/src/kimchi/control/utils.py
+++ b/src/kimchi/control/utils.py
@@ -69,13 +69,14 @@ def mime_in_header(header, mime):
 
 
 def parse_request():
-    if 'Content-Length' not in cherrypy.request.headers:
-        return {}
     rawbody = cherrypy.request.body.read()
 
     if mime_in_header('Content-Type', 'application/json'):
         try:
-            return json.loads(rawbody)
+            if cherrypy.request.body.length:
+                return json.loads(rawbody)
+            else:
+                return {}
         except ValueError:
             raise cherrypy.HTTPError(400, "Unable to parse JSON request")
     else:
-- 
1.8.4.2




More information about the Kimchi-devel mailing list