简体   繁体   中英

Cherrypy index dispatcher not like defined

This is my first outing with CherryPy so forgive any stupidity.

I'm trying to write a RESTful API that in part deals with adding/removing people. I want to be able to GET/PUT/DELETE example.com/people/.

The dispatcher seems to be behaving totally differently for the index method vs a defined function:

class people:
    """This is the class for CherryPy that deals with CRUD on people"""
    @cherrypy.expose
    def index(self, name):
        return name

    @cherrypy.expose
    def who(self, name):
        return name

root = webroot()
root.people = people()
cherrypy.quickstart(root)

If I call example.com/people/tom, I get a 404, if I call example.com/people/who/tom I get 'tom' returned.

Can anyone see what I'm doing wrong? Is there a way I can pass /xxx to index?

Indexes are a bit different when it comes to URL arguments.

The index method has a special role in CherryPy: it handles intermediate URI's that end in a slash; for example, the URI /orders/items/ might map to root.orders.items.index. The index method can take additional keyword arguments if the request includes querystring or POST params; see Keyword Arguments, next. However, unlike all other page handlers, it cannot take positional arguments

source

However, the url of example.com/people?name=tom should work as you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM