If the ``Content-Type`` header is ``application/json`` or ``application/json-rpc``, this property holds the parsed content of the request body. Only requests smaller than :attr:`MEMFILE_MAX` are processed to avoid memory exhaustion. Invalid JSON raise
(self)
| 1271 | |
| 1272 | @DictProperty('environ', 'bottle.request.json', read_only=True) |
| 1273 | def json(self): |
| 1274 | """ If the ``Content-Type`` header is ``application/json`` or |
| 1275 | ``application/json-rpc``, this property holds the parsed content |
| 1276 | of the request body. Only requests smaller than :attr:`MEMFILE_MAX` |
| 1277 | are processed to avoid memory exhaustion. |
| 1278 | Invalid JSON raises a 400 error response. |
| 1279 | """ |
| 1280 | ctype = self.environ.get('CONTENT_TYPE', '').lower().split(';')[0] |
| 1281 | if ctype in ('application/json', 'application/json-rpc'): |
| 1282 | b = self._get_body_string(self.MEMFILE_MAX) |
| 1283 | if not b: |
| 1284 | return None |
| 1285 | try: |
| 1286 | return json_loads(b) |
| 1287 | except (ValueError, TypeError): |
| 1288 | raise HTTPError(400, 'Invalid JSON') |
| 1289 | return None |
| 1290 | |
| 1291 | def _iter_body(self, read, bufsize): |
| 1292 | maxread = max(0, self.content_length) |
no test coverage detected