Friendly URL for a REST WebService with CherryPy

前端 未结 2 2003
终归单人心
终归单人心 2021-01-31 21:05

I\'m making a RESTful WebService using CherryPy 3 but I encounter a problem : I want to be able to answer requests like : /customers/1/products/386 meaning I wa

2条回答
  •  孤城傲影
    2021-01-31 21:38

    Thanks for your answer Sylvain. You've led me to the answer I was looking for. I used the RouteDispatcher like this :

        self.connect("cust_products", "/customers/{cust_id}/products/",
                     controller=CustomerController(),
                     action='index',
                     conditions=dict(method=['GET']))
    
        self.connect("cust_products", "/customers/{cust_id}/products/{id}",
                     controller=CustomerController(),
                     action='show',
                     conditions=dict(method=['GET']))
    
        self.connect("cust_products", "/customers/{cust_id}/products/",
                     controller=CustomerController(),
                     action='create',
                     conditions=dict(method=['POST']))
    
        self.connect("cust_products", "/customers/{cust_id}/products/{id}",
                     controller=CustomerController(),
                     action='update',
                     conditions=dict(method=['PUT']))
    
    
        self.connect("cust_products", "/customers/{cust_id}/products/{id}",
                     controller=CustomerController(),
                     action='delete',
                     conditions=dict(method=['DELETE']))
    

提交回复
热议问题