1 import sys
2 import cherrypy
3 from cherrypy._cpcompat import basestring, ntou, json, json_encode, json_decode
4
15
18 """Add a processor to parse JSON request entities:
19 The default processor places the parsed data into request.json.
20
21 Incoming request entities which match the given content_type(s) will
22 be deserialized from JSON to the Python equivalent, and the result
23 stored at cherrypy.request.json. The 'content_type' argument may
24 be a Content-Type string or a list of allowable Content-Type strings.
25
26 If the 'force' argument is True (the default), then entities of other
27 content types will not be allowed; "415 Unsupported Media Type" is
28 raised instead.
29
30 Supply your own processor to use a custom decoder, or to handle the parsed
31 data differently. The processor can be configured via
32 tools.json_in.processor or via the decorator method.
33
34 Note that the deserializer requires the client send a Content-Length
35 request header, or it will raise "411 Length Required". If for any
36 other reason the request entity cannot be deserialized from JSON,
37 it will raise "400 Bad Request: Invalid JSON document".
38
39 You must be using Python 2.6 or greater, or have the 'simplejson'
40 package importable; otherwise, ValueError is raised during processing.
41 """
42 request = cherrypy.serving.request
43 if isinstance(content_type, basestring):
44 content_type = [content_type]
45
46 if force:
47 if debug:
48 cherrypy.log('Removing body processors %s' %
49 repr(request.body.processors.keys()), 'TOOLS.JSON_IN')
50 request.body.processors.clear()
51 request.body.default_proc = cherrypy.HTTPError(
52 415, 'Expected an entity of content type %s' %
53 ', '.join(content_type))
54
55 for ct in content_type:
56 if debug:
57 cherrypy.log('Adding body processor for %s' % ct, 'TOOLS.JSON_IN')
58 request.body.processors[ct] = processor
59
63
87