In a RESTful application, how do we differentiate between an “action” and an HTTP verb (GET, POST, PUT, DELETE)?

前端 未结 2 419
温柔的废话
温柔的废话 2021-01-06 06:56

In a RESTful application, how do we differentiate between an \"action\" and an HTTP verb (GET, POST, PUT, DELETE)?

<
2条回答
  •  天命终不由人
    2021-01-06 07:05

    IMHO, the best option is to make the request method a part of controller's action.

    Lets say you are accessing http://who.cares/product/42 or http://who.cares/product/42/specification . This query to webserver would translate as Product controller. The actions name should be created by combining request method and command:

    DELETE "http://who.cares/product/42"
    
        controller: "Product", 
        action:     "deleteProduct()" 
    
    
    GET "http://who.cares/product/42/details"
    
        controller: "Product", 
        action:     "getDetails()"
    
    
    POST "http://who.cares/product/42/review"
    
        controller: "Product", 
        action:     "postReview()"
    
    
    GET "http://who.cares/products/ 
    
        controller: "Products", 
        action:     "getProducts()"
    
    
    POST "http://who.cares/products/ 
    
        controller: "Products", 
        action:     "postProducts()"
    

提交回复
热议问题