Web service differences between REST and RPC

后端 未结 9 1289
情歌与酒
情歌与酒 2020-12-07 06:54

I have a web service that accepts JSON parameters and have specific URLs for methods, e.g.:

http://IP:PORT/API/getAllData?p={JSON}

This is

9条回答
  •  旧巷少年郎
    2020-12-07 07:16

    As others have said, a key difference is that REST is noun-centric and RPC is verb-centric. I just wanted to include this clear table of examples demonstrating that:

    ---------------------------+-------------------------------------+--------------------------
     Operation                 | RPC (operation)                     | REST (resource)
    ---------------------------+-------------------------------------+--------------------------
     Signup                    | POST /signup                        | POST /persons           
    ---------------------------+-------------------------------------+--------------------------
     Resign                    | POST /resign                        | DELETE /persons/1234    
    ---------------------------+-------------------------------------+--------------------------
     Read person               | GET /readPerson?personid=1234       | GET /persons/1234       
    ---------------------------+-------------------------------------+--------------------------
     Read person's items list  | GET /readUsersItemsList?userid=1234 | GET /persons/1234/items 
    ---------------------------+-------------------------------------+--------------------------
     Add item to person's list | POST /addItemToUsersItemsList       | POST /persons/1234/items
    ---------------------------+-------------------------------------+--------------------------
     Update item               | POST /modifyItem                    | PUT /items/456          
    ---------------------------+-------------------------------------+--------------------------
     Delete item               | POST /removeItem?itemId=456         | DELETE /items/456       
    ---------------------------+-------------------------------------+--------------------------
    

    Notes

    • As the table shows, REST tends to use URL path parameters to identify specific resources
      (e.g. GET /persons/1234), whereas RPC tends to use query parameters for function inputs
      (e.g. GET /readPerson?personid=1234).
    • Not shown in the table is how a REST API would handle filtering, which would typically involve query parameters (e.g. GET /persons?height=tall).
    • Also not shown is how with either system, when you do create/update operations, additional data is probably passed in via the message body (e.g. when you do POST /signup or POST /persons, you include data describing the new person).
    • Of course, none of this is set in stone, but it gives you an idea of what you are likely to encounter and how you might want to organize your own API for consistency. For further discussion of REST URL design, see this question.

提交回复
热议问题