我有一个接受JSON参数的web服务,并有特定的方法url,例如:
http://IP:PORT/API/getAllData?p={JSON}
这肯定不是REST,因为它不是无状态的。它会考虑cookie,并有自己的会话。
是RPC吗?RPC和REST之间的区别是什么?
我有一个接受JSON参数的web服务,并有特定的方法url,例如:
http://IP:PORT/API/getAllData?p={JSON}
这肯定不是REST,因为它不是无状态的。它会考虑cookie,并有自己的会话。
是RPC吗?RPC和REST之间的区别是什么?
当前回答
正如其他人所说,一个关键的区别是REST url以名称为中心,而RPC url以动词为中心。我只是想包括这个清晰的例子表来证明:
---------------------------+-------------------------------------+-------------------------- 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 ---------------------------+-------------------------------------+--------------------------
笔记
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.
其他回答
REST最好被描述为与资源一起工作,而RPC更多地是关于操作。
休息 代表具象状态传输。这是一种组织独立系统之间交互的简单方法。 RESTful应用程序通常使用HTTP请求来发布数据(创建和/或更新)、读取数据(例如,进行查询)和删除数据。因此,REST可以将HTTP用于所有四个CRUD(创建/读取/更新/删除)操作。
RPC 主要用于跨不同模块通信,以服务用户请求。 例如,在openstack中,nova、glance和neutron在启动虚拟机时是如何协同工作的。
您不能仅仅通过查看您发布的内容来明确区分REST或RPC。
REST的一个约束是它必须是无状态的。如果你有一个会话,那么你就有一个状态,所以你不能把你的服务称为RESTful的。
事实上,您在URL中有一个操作(即getAllData)是对RPC的指示。在REST中,您交换表示,您执行的操作由HTTP谓词指定。同样,在REST中,内容协商不是使用?p={JSON}参数执行的。
不知道您的服务是否是RPC,但它不是RESTful的。你可以在网上了解它们的区别,这里有一篇文章可以让你开始:揭穿RPC和REST的神话。您更了解服务内部的内容,因此将其函数与RPC进行比较,并得出自己的结论。
在HTTP上,它们都只是HttpRequest对象,它们都期望返回一个HttpResponse对象。我认为人们可以继续用这种描述来编码,而不用担心其他的事情。
这是我在不同的用例中理解和使用它们的方式:
例子:餐厅管理
REST的用例:订单管理
- create order (POST), update order (PATCH), cancel order (DELETE), retrieve order (GET)
- endpoint: /order?orderId=123
对于资源管理,REST是干净的。一个具有预定义操作的端点。这可以看作是一种向世界公开DB (Sql或NoSql)或类实例的方法。
实现的例子:
class order:
on_get(self, req, resp): doThis.
on_patch(self, req, resp): doThat.
框架示例:用于python的Falcon。
RPC的用例:操作管理
- prepare ingredients: /operation/clean/kitchen
- cook the order: /operation/cook/123
- serve the order /operation/serve/123
对于分析性的、可操作的、非响应性的、非代表性的、基于动作的作业,RPC工作得更好,并且很自然地认为它是功能性的。
实现的例子:
@route('/operation/cook/<orderId>')
def cook(orderId): doThis.
@route('/operation/serve/<orderId>')
def serve(orderId): doThat.
框架示例:Flask for python
正如其他人所说,一个关键的区别是REST url以名称为中心,而RPC url以动词为中心。我只是想包括这个清晰的例子表来证明:
---------------------------+-------------------------------------+-------------------------- 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 ---------------------------+-------------------------------------+--------------------------
笔记
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.