RESTful API 设计规范整理

2019-02-15  本文已影响0人  IT513

[TOC]


HTTP 请求规范

HTTP 状态码

状态码范围

服务器向用户返回的状态码和提示信息



URL 规范

域名



版本

https://api.example.com/v1/
https://example.com/api/v1/



命名

GET         /zoos:列出所有动物园
POST        /zoos:新建一个动物园
GET         /zoos/ID:获取某个指定动物园的信息
PUT         /zoos/ID:更新某个指定动物园的信息(提供该动物园的全部信息)
PATCH       /zoos/ID:更新某个指定动物园的信息(提供该动物园的部分信息)
DELETE      /zoos/ID:删除某个动物园
GET         /zoos/ID/animals:列出某个指定动物园的所有动物
DELETE      /zoos/ID/animals/ID:删除某个指定动物园的指定动物
DELETE      /zoos/ID/animals?id=:删除某个指定动物园的指定动物



返回数据规范

返回错误格式

{
    status:400,
    error_code:40006
    error: "Invalid API key"
}

错误码参考:
新浪错误码
微信错误码
豆瓣restful api 状态和错误码



** 返回结果格式 **

{
    status:200,
    data: []||{},
    msg:'提示信息'
}



** Hypermedia API **

{
current_user_url: "https://api.github.com/user",
current_user_authorizations_html_url: "https://github.com/settings/connections/applications{/client_id}",
authorizations_url: "https://api.github.com/authorizations",
code_search_url: "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}",
commit_search_url: "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}",
emails_url: "https://api.github.com/user/emails",
...

{
  "message": "Requires authentication",
  "documentation_url": "https://developer.github.com/v3"
}

上面代码表示,服务器给出了提示信息,以及文档的网址。



参考文章

RESTful API 设计指南
微服务指南走北(三):Restful API 设计简述
RESTful API 规范 v1.0
网上整理的对于Rest和Restful api的理解



上一篇 下一篇

猜你喜欢

热点阅读