restful规范:
API与用户的通讯协议,老是使用HTTPs协议。
- URL
https://api.example.com 尽可能将API部署在专用域名(会存在跨域问题)html
https://example.org/api/ API很简单python
- url名词
路径,视网络上任何东西都是资源,均使用名词表示(可复数)django
https://api.example.com/v1/zoosjson
https://api.example.com/v1/animalsapi
https://api.example.com/v1/employees跨域
- 版本(v1/v2/……)
- 提交方式
GET: 获取
POST: 添加
PUT: 更新
DELETE:删除
- status
2xx:OK
3xx:重定向
4xx:用户发出的请求有错误
5xx:服务器发生错误
更多看这里:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
中文看这里:http://www.cnblogs.com/yifugui/p/8416123.html
- Hypermedia link
RESTful API最好作到Hypermedia,即返回结果中提供连接,连向其余API方法,使得用户不查文档,也知道下一步应该作什么。
{"link": { "rel": "collection https://www.example.com/zoos", "href": "https://api.example.com/zoos", "title": "List of zoos", "type": "application/vnd.yourformat+json" }}
- 错误详细
- 返回结果,针对不一样操做,服务器向用户返回的结果应该符合如下规范。
GET /collection:返回资源对象的列表(数组) GET /collection/resource:返回单个资源对象 POST /collection:返回新生成的资源对象 PUT /collection/resource:返回完整的资源对象 PATCH /collection/resource:返回完整的资源对象 DELETE /collection/resource:返回一个空文档
路由系统:数组
1
2
3
|
urlpatterns
=
[
url(r
'^users'
, Users.as_view()),
]
|
CBV视图:服务器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
from
django.views
import
View
from
django.http
import
JsonResponse
class
Users(View):
def
get(
self
, request,
*
args,
*
*
kwargs):
result
=
{
'status'
:
True
,
'data'
:
'response data'
}
return
JsonResponse(result, status
=
200
)
def
post(
self
, request,
*
args,
*
*
kwargs):
result
=
{
'status'
:
True
,
'data'
:
'response data'
}
return
JsonResponse(result, status
=
200
)
|
更多查看:http://www.cnblogs.com/wupeiqi/articles/7805382.htmlrestful