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

Sheldon shaohef at linux.vnet.ibm.com
Wed Feb 12 08:41:34 UTC 2014


On 02/12/2014 02:30 PM, shaohef at linux.vnet.ibm.com wrote:
> 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 {}
We can remain this for GET and DELETE method.
and return None as follow.

     if 'Content-Length' not in cherrypy.request.headers:
+        return None


But as the commit message,  GET will not get parameters from body.
GET will get parameters for query-parameters.

And here 'Content-Length' is useful for PUT and POST method.

So I remove this.

>       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:


-- 
Thanks and best regards!

Sheldon Feng(冯少合)<shaohef at linux.vnet.ibm.com>
IBM Linux Technology Center




More information about the Kimchi-devel mailing list