Home | Trees | Indices | Help |
|
---|
|
1 import cherrypy 2 from cherrypy._cpcompat import basestring, ntou, json_encode, json_decode 3 46 """Read application/json data into request.json.""" 7 if not entity.headers.get(ntou("Content-Length"), ntou("")): 8 raise cherrypy.HTTPError(411) 9 10 body = entity.fp.read() 11 try: 12 cherrypy.serving.request.json = json_decode(body.decode('utf-8')) 13 except ValueError: 14 raise cherrypy.HTTPError(400, 'Invalid JSON document')15 1617 -def json_in(content_type=[ntou('application/json'), ntou('text/javascript')], 18 force=True, debug=False, processor=json_processor):19 """Add a processor to parse JSON request entities: 20 The default processor places the parsed data into request.json. 21 22 Incoming request entities which match the given content_type(s) will 23 be deserialized from JSON to the Python equivalent, and the result 24 stored at cherrypy.request.json. The 'content_type' argument may 25 be a Content-Type string or a list of allowable Content-Type strings. 26 27 If the 'force' argument is True (the default), then entities of other 28 content types will not be allowed; "415 Unsupported Media Type" is 29 raised instead. 30 31 Supply your own processor to use a custom decoder, or to handle the parsed 32 data differently. The processor can be configured via 33 tools.json_in.processor or via the decorator method. 34 35 Note that the deserializer requires the client send a Content-Length 36 request header, or it will raise "411 Length Required". If for any 37 other reason the request entity cannot be deserialized from JSON, 38 it will raise "400 Bad Request: Invalid JSON document". 39 40 You must be using Python 2.6 or greater, or have the 'simplejson' 41 package importable; otherwise, ValueError is raised during processing. 42 """ 43 request = cherrypy.serving.request 44 if isinstance(content_type, basestring): 45 content_type = [content_type] 46 47 if force: 48 if debug: 49 cherrypy.log('Removing body processors %s' % 50 repr(request.body.processors.keys()), 'TOOLS.JSON_IN') 51 request.body.processors.clear() 52 request.body.default_proc = cherrypy.HTTPError( 53 415, 'Expected an entity of content type %s' % 54 ', '.join(content_type)) 55 56 for ct in content_type: 57 if debug: 58 cherrypy.log('Adding body processor for %s' % ct, 'TOOLS.JSON_IN') 59 request.body.processors[ct] = processor60 6163 value = cherrypy.serving.request._json_inner_handler(*args, **kwargs) 64 return json_encode(value)65 6669 """Wrap request.handler to serialize its output to JSON. Sets Content-Type. 70 71 If the given content_type is None, the Content-Type response header 72 is not set. 73 74 Provide your own handler to use a custom encoder. For example 75 cherrypy.config['tools.json_out.handler'] = <function>, or 76 @json_out(handler=function). 77 78 You must be using Python 2.6 or greater, or have the 'simplejson' 79 package importable; otherwise, ValueError is raised during processing. 80 """ 81 request = cherrypy.serving.request 82 # request.handler may be set to None by e.g. the caching tool 83 # to signal to all components that a response body has already 84 # been attached, in which case we don't need to wrap anything. 85 if request.handler is None: 86 return 87 if debug: 88 cherrypy.log('Replacing %s with JSON handler' % request.handler, 89 'TOOLS.JSON_OUT') 90 request._json_inner_handler = request.handler 91 request.handler = handler 92 if content_type is not None: 93 if debug: 94 cherrypy.log('Setting Content-Type to %s' % 95 content_type, 'TOOLS.JSON_OUT') 96 cherrypy.serving.response.headers['Content-Type'] = content_type97
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Tue Dec 2 09:59:42 2014 | http://epydoc.sourceforge.net |