Servicio web Cherrypy de excepción 415
Estoy tratando de construir un servicio web Cherrypy / Python. Ya paso todo el día descubriendo cómo hacer posible una solicitud de dominio cruzado ajax. Eso finalmente funciona, pero ahora tengo el próximo problema. Creo que ya conozco la solución, pero no sé cómo implementarla. El problema es que cuando envío la solicitud ajax, el servidor Cherrypy responde con:
415 Unsupported Media Type
Expected an entity of content type application/json, text/javascript
Traceback (most recent call last): File "/Library/Python/2.7/site-packages/cherrypy/_cprequest.py", line 663, in respond self.body.process() File "/Library/Python/2.7/site-packages/cherrypy/_cpreqbody.py", line 996, in process super(RequestBody, self).process() File "/Library/Python/2.7/site-packages/cherrypy/_cpreqbody.py", line 538, in process self.default_proc() File "/Library/Python/2.7/site-packages/cherrypy/_cperror.py", line 411, in __call__ raise selfHTTPError: (415, u'Expected an entity of content type application/json, text/javascript')
La solución que encontré y que trato de probar es agregar esta línea a la configuración:
'tools.json_in.force': False
Así que intenté implementarlo en este código:
import cherrypy
import json
import sys
class RelatedDocuments:
def index(self):
return "Hello World!"
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def findRelated(self, **raw):
#Get JSON message form request
request = cherrypy.request.json
result = []
#SOME CODE...
return result;
# Expose the index method through the web. CherryPy will never
# publish methods that don't have the exposed attribute set to True.
index.exposed = True
findRelated.exposed = True
def CORS():
cherrypy.response.headers["Access-Control-Allow-Origin"] = "*"
import os.path
tutconf = os.path.join(os.path.dirname(__file__), 'webserver.conf')
config = {
'global': {
'server.socket_host':'127.0.0.1',
'server.socket_port': 8080,
'log.error_file' : 'Web.log',
'log.access_file' : 'Access.log'
},
'/': {
'tools.CORS.on': True
}
}
if __name__ == '__main__':
cherrypy.tools.CORS = cherrypy.Tool('before_finalize', CORS)
cherrypy.quickstart(RelatedDocuments(),config=config)
Agregué la línea de configuración debajo de la línea tools.CORS.on, pero eso no funcionó. Luego probé esto:
cherrypy.config.update({
'tools.json_in.force': False,
});
No funcionó antes ... a continuación intenté implementar esto justo encima del método findRelated:
@cherrypy.config(**{'tools.json_in.force': False})
Todas las implementaciones me dieron un error 500, realmente agradezco si alguien puede ayudarme. ¡Gracias por adelantado!