Search code examples
pythoncherrypy

Path Not Found in CherryPy


I've been working on implementing a test API in CherryPy.

I've read a few forums, tutorials and pieced together the code that the old Python developer at work had written and this is what I've got:

import json
import cherrypy

class person:
    def default(self, *args):
        r = {
            'firstName': args[0],
            'lastName': args[1],
            'age': args[2]
        }
        return json.dumps(r)
    default.exposed = True

class api:
    def __init__(self):
        self.person = person()

class Root:
    def __init__(self):
        self.api = api()

conf = {
    '/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}

cherrypy.config.update(conf)
cherrypy.tree.mount(Root()) 
cherrypy.quickstart()

The result of running this code is the following error:

404 Not Found

The path '/api/person/Blake/Williams/27' was not found.

Traceback (most recent call last): File "/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 188, in call self.body = self.oldhandler(*args, **kwargs) File "/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/_cperror.py", line 386, in call raise self

NotFound: (404, "The path '/api/person/Blake/Williams/27' was not found.")

I'm not sure what I'm doing wrong... Thanks in advance for any help.


Solution

  • If you start your cherrypy server like this, then it works:

    cherrypy.quickstart(cherrypy.Application(Root()), '/', {})
    

    ! Caution: The path you indicate is still not found: you forget the api level:

    http://127.0.0.1:8080/api/person/Blake/Williams/27