假设我在https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/上使用两个端点( spaceballs/raspberry和spaceballs/technician )来模拟REST服务器。这些端点将响应HTTP请求,其中包含来自Spaceball的引号,如
curl -X GET "https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/spaceballs/raspberry"
Only one man would dare give me the raspberry!和
curl -X GET "https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/spaceballs/technician"
We got the bleeps, sweeps and the creeps!所以这一切都很好,但是如果我不知道这两个端点,仅仅是RESTful服务器的URL呢?我怎么能问这个服务器(在这个例子中,是被嘲弄的)服务器有什么资源?
发布于 2019-07-08 09:03:27
作为一个客户,你不应该知道这两个端点!这是REST的基本原则之一,您的资源应该是可发现的。
了解https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/的端点,您的用户应该能够使用超媒体在API中导航。实际上,这意味着您的入口点返回到其他相关资源的链接。因此,你应该有这样的东西;
GET https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/
{
"self": {
"type": "link.list",
"uri": "/",
"href": "https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/"
},
"links": [
{
"rel": "film",
"type": "link.list",
"href": "https://https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/spaceballs"
}
]
}上面的响应告诉客户端,当前URI是一个链接列表,并且有一个指向主机/spaceballs中的另一个资源的链接(称为“胶片”),该资源也是一个链接列表。使用这些信息,客户端可以调用该API;
GET https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/spaceballs
{
"self": {
"type": "link.list",
"uri": "/spaceballs",
"href": "https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/spaceballs"
},
"links": [
{
"rel": "quote",
"type": "film.quote",
"href": "https://https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/spaceballs/raspberry"
},
{
"rel": "quote",
"type": "film.quote",
"href": "https://https://50281ac6-ece9-42d3-9590-8e3325f50bd9.mock.pstmn.io/spaceballs/technician"
}
]
}您现在已经使用超媒体来描述应用程序的状态,并且正在编写REST!这是非常非常重要的一点。像这样的描述性链接并不是“很好的选择”--它们是必需的。如果您的API不是这样工作的,那么它就不是REST。
https://stackoverflow.com/questions/56927246
复制相似问题