每个Web应用程序 - 每个Web站点 - 都是一项服务.(...)使网站易于使用的网站功能也使网络服务API易于程序员使用.
Richardson和Ruby,"RESTFul Web Services"
根据我的意图,一个也是Web服务的Web站点提供其资源的多种表示,具体取决于用户代理请求的内容.可以这么说的API是网站本身,不是单独提供的.
对于许多流行的"REST API"而言,情况并非如此.例如,Twitter的API位于http://api.twitter.com/1/,URI中的"1"是API本身的版本.Socialcast还在https://demo.socialcast.com/api/上提供了REST API ,第三级名称是它所处理的网络的名称.
这对我来说似乎不对.如果我的博客位于http://www.example.com/blog,我不需要在其他位置提供API,仅为机器人提供JSON.而不是http://www.example.com/blog/posts/和http://api.example.com/blog/posts,两个不同的URI,我应该只有前者,并且可以使用多种表示,其中application/json
对于我希望提供给我的用户的JSON API.
示例1:浏览器询问我博客上的帖子;
请求:
curl -i \
-H "Accept: text/html" \
-X GET \
http://www.example.org/blog/posts/
Run Code Online (Sandbox Code Playgroud)
响应:
200 OK
Content-Type: text/html; charset=utf-8
<html><body><h1>Posts</h1><ol><li><h2>My first post ...
Run Code Online (Sandbox Code Playgroud)
示例2:相同的URI,但这次机器人发出请求;
请求:
curl -i \
-H "Accept: application/json" \
-X GET \
http://www.example.org/blog/posts/
Run Code Online (Sandbox Code Playgroud)
响应:
200 OK
Content-Type: text/html; charset=utf-8
{
"posts": [
{
"id": 1,
"title": "My first …
Run Code Online (Sandbox Code Playgroud)