REST(一)设计风格

rest(一)

常见的rest的实践,主要参考github的api文档,下面总结的只是常用的部分,待补充。

协议

版本

路径

HTTP动词

常用的HTTP动词有下面五个(括号里是对应的SQL命令)。

返回结果

Schema

返回的码字

认证授权

Basic authentication

curl -u "username" https://api.github.com

OAuth2 token (sent in a header)

curl -H "Authorization: token OAUTH-TOKEN" https://api.github.com

OAuth2 token (sent as a parameter)

curl https://api.github.com/?access_token=OAUTH-TOKEN

分页

默认情况下,如不指定页数,多个条目返回,返回30条。

Hypermedia

这点也是rest的精髓:

All resources may have one or more *_url properties linking to other resources. These are meant to provide explicit URLs so that proper API clients don’t need to construct URLs on their own. It is highly recommended that API clients use these. Doing so will make future upgrades of the API easier for developers. All URLs are expected to be proper RFC 6570 URI templates.

参考

github api
这样设计 RESTful API,也许能让你效率倍增
RESTful API 设计指南

Table of Contents