[PATCH 4 of 6 rqlcontroller] [py3] Decode request's content before JSON loading it

Denis Laxalde denis.laxalde at logilab.fr
Mon Jun 4 14:28:43 CEST 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1528114494 -7200
#      Mon Jun 04 14:14:54 2018 +0200
# Node ID 59eab509372ef275e27d08691da0809fa68b0b0d
# Parent  35e5a74d2bcc289b9444388601e53d7f00c6d1d7
# Available At http://hg.logilab.org/review/cubes/rqlcontroller
#              hg pull http://hg.logilab.org/review/cubes/rqlcontroller -r 59eab509372e
# EXP-Topic py3
[py3] Decode request's content before JSON loading it

In Python3, json.load() only accepts text content. So we decode
_cw.content's value using request's encoding before passing it to
json.load(). As a consequence of using .getvalue() on BytesIO (content
is a BytesIO), we no longer need to .seek() after reading the content.

diff --git a/views.py b/views.py
--- a/views.py
+++ b/views.py
@@ -107,8 +107,8 @@ class RqlIOController(Controller):
             contenttype = self._cw.get_header('Content-Type', raw=False)
             if (contenttype.mediaType, contenttype.mediaSubtype) == ('application', 'json'):  # noqa: E501
                 encoding = contenttype.params.get('charset', 'utf-8')
-                args = json.load(self._cw.content, encoding=encoding)
-                self._cw.content.seek(0, 0)
+                content = self._cw.content.getvalue().decode(encoding)
+                args = json.loads(content)
             else:
                 args = json.load(self._cw.form['json'][1])
 


More information about the cubicweb-devel mailing list