Parse the already-read body in _json_body
This commit is contained in:
1 parent
6315c49a4e
commit
7c8b6b78ed
1 file changed
+7
-4
@@ -295,13 +295,16 @@ class SkuldHandler(BaseHTTPRequestHandler):
|
|||||||
return self._send_json(payload, cookies=self._pending_cookies)
|
return self._send_json(payload, cookies=self._pending_cookies)
|
||||||
raise ApiError(404, "Fann ikkje det du spurde etter.")
|
raise ApiError(404, "Fann ikkje det du spurde etter.")
|
||||||
|
|
||||||
def _json_body(self):
|
def _read_body(self):
|
||||||
|
"""Read the request body once, before any answer is written."""
|
||||||
length = int(self.headers.get("Content-Length") or 0)
|
length = int(self.headers.get("Content-Length") or 0)
|
||||||
if length <= 0:
|
return self.rfile.read(length) if length > 0 else b""
|
||||||
|
|
||||||
|
def _json_body(self):
|
||||||
|
if not self._raw_body:
|
||||||
return {}
|
return {}
|
||||||
raw = self.rfile.read(length)
|
|
||||||
try:
|
try:
|
||||||
data = json.loads(raw.decode("utf-8"))
|
data = json.loads(self._raw_body.decode("utf-8"))
|
||||||
except (UnicodeDecodeError, ValueError):
|
except (UnicodeDecodeError, ValueError):
|
||||||
raise ApiError(400, "Ugyldig JSON i førespurnaden.")
|
raise ApiError(400, "Ugyldig JSON i førespurnaden.")
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
|
|||||||
Reference in new issue
Block a user