開頭直接給網站,阮一峯老師介紹東西就是到位:http://www.ruanyifeng.com/blog/2011/09/restful.html
當然了規範歸規範,但是大家在實際工作中,爲了方便自己項目的開發,會結合自己情況,對規範做出調整形成自己的接口模式,
畢竟沒有硬性要求。
下面介紹一下自己一套定義方式:
①:定義兩種請求類型:get(查詢),post(增,刪,改)
②:路徑命名規範: 全部小寫,禁止大寫和下劃線
錯誤示範:
http:://127.0.0.1/api/v1/content/wordList?id=1
http:://127.0.0.1/api/v1/content/word_list?id=1
③:顯式的標誌出是什麼請求(畢竟比讓人看get請求這種隱式的規範才知道是查詢來的實在)
顯式標識動詞:
新增:add
更新:update
刪除:delete
查詢:get
http:://127.0.0.1/api/v1/content/word/getlist?wordId=1 或者 http://127.0.0.1/api/v1/content/getwordlist?wordId=1
http:://127.0.0.1/api/v1/content/word/add 或者 http://127.0.0.1/api/v1/content/addword
http:://127.0.0.1/api/v1/content/word/update或者 http://127.0.0.1/api/v1/content/updateword
http:://127.0.0.1/api/v1/content/word/delete或者 http://127.0.0.1/api/v1/content/deleteword
如果是批量修改:
http:://127.0.0.1/api/v1/content/words/update或者 http://127.0.0.1/api/v1/content/updatewords
④:禁止使用無意義的單詞,例如:index,event,home.類似這種
http:://127.0.0.1/api/v1/content/index
http:://127.0.0.1/api/v1/content/xxxxx
⑤:版本化
加個v1,v2,什麼的,這個東西很有必要,就是在迭代或者更換接口的時候很有好處
備註:
參加其他各大服務商提供出來的api接口,對比了一下,種類很多如下,目測沒有什麼規律可言,如果你有好的資料或者想法請留言交流哈。
http://japi.juhe.cn/enterprise/getDetailByName
http://v.juhe.cn/shortLetter/submitTpl.php
http://op.juhe.cn/jiepai/oilcardrr/onlineorder
http://api.juheapi.com/japi/beian
https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_list
https://api.weixin.qq.com/cgi-bin/clear_quota