Finally I solved it. The weird thing was that I had to expose index
method (and all other methods in Root
class) using annotation @cherrypy.expose
and not just by setting exposed = True
like in RestAPI
class. I don't know why.
To properly test POST handler I didn't have to pass any variables but still I had to set Content-length: 0
header.
class Root(object):
@cherrypy.expose
def index(self):
return 'Hello world'
class RestAPI(object):
exposed = True
def POST(self):
return 'post'
def GET(self):
return 'get'
cherrypy.config.update({
'global': {
'environment': 'test_suite',
'server.socket_host': '127.0.0.1',
'server.socket_port': 8080,
}
})
cherrypy.tree.mount(Root())
cherrypy.tree.mount(RestAPI(), '/api',
{'/':
{'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}
)
cherrypy.engine.start()
cherrypy.engine.block()
Proper way to test POST using cURL:
curl -X POST --header "Content-length: 0" http://localhost:8080/api