{"id":225,"date":"2015-05-13T15:25:47","date_gmt":"2015-05-13T15:25:47","guid":{"rendered":"http:\/\/blog.chen-hongyi.com\/?p=225"},"modified":"2015-07-02T07:32:59","modified_gmt":"2015-07-02T07:32:59","slug":"225","status":"publish","type":"post","link":"https:\/\/chen-hongyi.com\/?p=225","title":{"rendered":"[\u8f6c]RESTful API \u8bbe\u8ba1\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<p>\u6765\u6e90: http:\/\/blog.jobbole.com\/41233\/<\/p>\n<div class=\"entry-header\">\n<h1>RESTful API \u8bbe\u8ba1\u6700\u4f73\u5b9e\u8df5<\/h1>\n<\/div>\n<div class=\"entry-meta\">\n<p class=\"entry-meta-hide-on-mobile\">2013\/07\/13 | \u5206\u7c7b\uff1a <a title=\"\u67e5\u770b \u5f00\u53d1 \u4e2d\u7684\u5168\u90e8\u6587\u7ae0\" href=\"http:\/\/blog.jobbole.com\/category\/programmer\/\" rel=\"category tag\">\u5f00\u53d1<\/a> | <a title=\"\u300aRESTful API \u8bbe\u8ba1\u6700\u4f73\u5b9e\u8df5\u300b\u4e0a\u7684\u8bc4\u8bba\" href=\"http:\/\/blog.jobbole.com\/41233\/#comments\">6 \u6761\u8bc4\u8bba<\/a> | \u6807\u7b7e\uff1a <a href=\"http:\/\/blog.jobbole.com\/tag\/api\/\">API<\/a><\/p>\n<div class=\"jiathis_style\"><span class=\"jiathis_txt\">\u5206\u4eab\u5230\uff1a<\/span><a class=\"jiathis_counter_style\"><span id=\"jiathis_counter_75\" class=\"jiathis_button_expanded jiathis_counter jiathis_bubble_style\" title=\"\u7d2f\u8ba1\u5206\u4eab94\u6b21\">94<\/span><\/a><\/div>\n<\/div>\n<div class=\"entry\">\n<div class=\"imooc\">\n<ul>\n<li><a title=\"\u5feb\u4e50\u7684sublime\u7f16\u8f91\u5668\" href=\"http:\/\/www.imooc.com\/view\/333?from=jobboleblog\" target=\"_blank\">\u5feb\u4e50\u7684sublime\u7f16\u8f91\u5668<\/a><\/li>\n<li><a title=\"\u73a9\u8f6cKISSY\u6846\u67b6\" href=\"http:\/\/www.imooc.com\/view\/191?from=jobboleblog\" target=\"_blank\">\u73a9\u8f6cKISSY\u6846\u67b6<\/a><\/li>\n<li><a title=\"JAVA\u5b9e\u73b0\u5bf9\u79f0\u52a0\u5bc6\" href=\"http:\/\/www.imooc.com\/view\/287?from=jobboleblog\" target=\"_blank\">JAVA\u5b9e\u73b0\u5bf9\u79f0\u52a0\u5bc6<\/a><\/li>\n<li><a title=\"JAVA\u5b9e\u73b0\u975e\u5bf9\u79f0\u52a0\u5bc6\" href=\"http:\/\/www.imooc.com\/view\/288?from=jobboleblog\" target=\"_blank\">JAVA\u5b9e\u73b0\u975e\u5bf9\u79f0\u52a0\u5bc6<\/a><\/li>\n<\/ul>\n<\/div>\n<div class=\"copyright-area\">\u672c\u6587\u7531 <a href=\"http:\/\/blog.jobbole.com\/\">\u4f2f\u4e50\u5728\u7ebf<\/a> &#8211; <a href=\"http:\/\/blog.jobbole.com\/author\/bruce-accumulate\/\">bruce-accumulate<\/a> \u7ffb\u8bd1\u3002\u672a\u7ecf\u8bb8\u53ef\uff0c\u7981\u6b62\u8f6c\u8f7d\uff01<br \/>\n\u82f1\u6587\u51fa\u5904\uff1a<a href=\"http:\/\/www.vinaysahni.com\/best-practices-for-a-pragmatic-restful-api\" target=\"_blank\">Vinay Sahni<\/a>\u3002\u6b22\u8fce\u52a0\u5165<a href=\"http:\/\/www.jobbole.com\/groups\/6\/?utm_source=jobboleblog-article\" target=\"_blank\">\u7ffb\u8bd1\u5c0f\u7ec4<\/a>\u3002<\/div>\n<p><strong>\u80cc\u666f<\/strong><\/p>\n<p>\u76ee\u524d\u4e92\u8054\u7f51\u4e0a\u5145\u65a5\u7740\u5927\u91cf\u7684\u5173\u4e8eRESTful API\uff08\u4e3a\u65b9\u4fbf\uff0c\u4e0b\u6587\u4e2d\u201cRESTful API \u201d\u7b80\u5199\u4e3a\u201cAPI\u201d\uff09\u5982\u4f55\u8bbe\u8ba1\u7684\u6587\u7ae0\uff0c\u7136\u800c\u5374\u6ca1\u6709\u4e00\u4e2a\u201d\u4e07\u80fd\u201c\u7684\u8bbe\u8ba1\u6807\u51c6\uff1a\u5982\u4f55\u9274\u6743\uff1fAPI \u683c\u5f0f\u5982\u4f55\uff1f\u4f60\u7684API\u662f\u5426\u5e94\u8be5\u52a0\u5165\u7248\u672c\u4fe1\u606f\uff1f\u5f53\u4f60\u5f00\u59cb\u5199\u4e00\u4e2aapp\u7684\u65f6\u5019\uff0c\u7279\u522b\u662f\u540e\u7aef\u6a21\u578b\u90e8\u5206\u5df2\u7ecf\u5199\u5b8c\u7684\u65f6\u5019\uff0c\u4f60\u4e0d\u5f97\u4e0d\u6b9a\u7cbe\u7aed\u8651\u7684\u8bbe\u8ba1\u548c\u5b9e\u73b0\u81ea\u5df1app\u7684public API\u90e8\u5206\u3002\u56e0\u4e3a\u4e00\u65e6\u53d1\u5e03\uff0c\u5bf9\u5916\u53d1\u5e03\u7684API\u5c06\u4f1a\u5f88\u96be\u6539\u53d8\u3002<\/p>\n<p>\u5728\u7ed9SupportedFu\u8bbe\u8ba1API\u7684\u65f6\u5019\uff0c\u6211\u8bd5\u56fe\u4ee5\u5b9e\u7528\u7684\u89d2\u5ea6\u6765\u89e3\u51b3\u4e0a\u9762\u63d0\u5230\u7684\u95ee\u9898\u3002\u6211\u5e0c\u671b\u53ef\u4ee5\u8bbe\u8ba1\u51fa\u5bb9\u6613\u4f7f\u7528\uff0c\u5bb9\u6613\u90e8\u7f72\uff0c\u5e76\u4e14\u8db3\u591f\u7075\u6d3b\u7684API\uff0c\u672c\u6587\u56e0\u6b64\u800c\u751f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>API\u8bbe\u8ba1\u7684\u57fa\u672c\u8981\u6c42<\/strong><\/p>\n<p>\u7f51\u4e0a\u7684\u5f88\u591a\u5173\u4e8eAPI\u8bbe\u8ba1\u7684\u89c2\u70b9\u90fd\u5341\u5206\u201d\u5b66\u9662\u6d3e\u201c\uff0c\u5b83\u4eec\u4e5f\u8bb8\u66f4\u6709\u7406\u8bba\u57fa\u7840\uff0c\u4f46\u662f\u6709\u65f6\u5374\u548c\u73b0\u5b9e\u4e16\u754c\u8131\u8f68\uff08\u56e0\u6b64\u6211\u662f\u81ea\u7531\u6d3e\uff09\u3002\u6240\u4ee5\u6211\u8fd9\u7bc7\u6587\u7ae0\u7684\u76ee\u6807\u662f\u4ece\u5b9e\u8df5\u7684\u89d2\u5ea6\u51fa\u53d1\uff0c\u7ed9\u51fa\u5f53\u524d\u7f51\u7edc\u5e94\u7528\u7684API\u8bbe\u8ba1\u6700\u4f73\u5b9e\u8df5\uff08\u5f53\u7136\uff0c\u662f\u6211\u8ba4\u4e3a\u7684\u6700\u4f73\u4e86~\uff09\uff0c\u5982\u679c\u89c9\u5f97\u4e0d\u5408\u9002\uff0c\u6211\u4e0d\u4f1a\u9075\u4ece\u6807\u51c6\u3002\u5f53\u7136\u4f5c\u4e3a\u8bbe\u8ba1\u7684\u57fa\u7840\uff0c\u51e0\u4e2a\u5fc5\u987b\u7684\u539f\u5219\u8fd8\u662f\u8981\u9075\u5b88\u7684\uff1a<\/p>\n<ol>\n<li>\u5f53\u6807\u51c6\u5408\u7406\u7684\u65f6\u5019\u9075\u5b88\u6807\u51c6\u3002<\/li>\n<li>API\u5e94\u8be5\u5bf9\u7a0b\u5e8f\u5458\u53cb\u597d\uff0c\u5e76\u4e14\u5728\u6d4f\u89c8\u5668\u5730\u5740\u680f\u5bb9\u6613\u8f93\u5165\u3002<\/li>\n<li>API\u5e94\u8be5\u7b80\u5355\uff0c\u76f4\u89c2\uff0c\u5bb9\u6613\u4f7f\u7528\u7684\u540c\u65f6\u4f18\u96c5\u3002<\/li>\n<li>API\u5e94\u8be5\u5177\u6709\u8db3\u591f\u7684\u7075\u6d3b\u6027\u6765\u652f\u6301\u4e0a\u5c42ui\u3002<\/li>\n<li>API\u8bbe\u8ba1\u6743\u8861\u4e0a\u8ff0\u51e0\u4e2a\u539f\u5219\u3002<\/li>\n<\/ol>\n<p>\u9700\u8981\u5f3a\u8c03\u7684\u662f\uff1aAPI\u7684\u5c31\u662f\u7a0b\u5e8f\u5458\u7684UI\uff0c\u548c\u5176\u4ed6UI\u4e00\u6837\uff0c\u4f60\u5fc5\u987b\u4ed4\u7ec6\u8003\u8651\u5b83\u7684\u7528\u6237\u4f53\u9a8c\uff01<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u4f7f\u7528RESTful URLs \u548caction<\/strong>.<\/p>\n<p>\u867d\u7136\u524d\u9762\u6211\u8bf4\u6ca1\u6709\u4e00\u4e2a\u4e07\u80fd\u7684API\u8bbe\u8ba1\u6807\u51c6\u3002\u4f46\u786e\u5b9e\u6709\u4e00\u4e2a\u88ab\u666e\u904d\u627f\u8ba4\u548c\u9075\u5b88\uff1aRESTfu\u8bbe\u8ba1\u539f\u5219\u3002\u5b83\u88abRoy Felding\u63d0\u51fa\uff08\u5728\u4ed6\u7684\u201d\u57fa\u4e8e\u7f51\u7edc\u7684\u8f6f\u4ef6\u67b6\u6784\u201c\u8bba\u6587\u4e2d<a href=\"http:\/\/www.ics.uci.edu\/~fielding\/pubs\/dissertation\/rest_arch_style.htm\" target=\"_blank\">\u7b2c\u4e94\u7ae0<\/a>\uff09\u3002\u800cREST\u7684\u6838\u5fc3\u539f\u5219\u662f\u5c06\u4f60\u7684API\u62c6\u5206\u4e3a\u903b\u8f91\u4e0a\u7684\u8d44\u6e90\u3002\u8fd9\u4e9b\u8d44\u6e90\u901a\u8fc7http\u88ab\u64cd\u4f5c\uff08GET ,POST,PUT,DELETE\uff09\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u90a3\u4e48\u6211\u5e94\u8be5\u5982\u4f55\u62c6\u5206\u51fa\u8fd9\u4e9b\u8d44\u6e90\u5462\uff1f<\/strong><\/p>\n<p>\u663e\u7136\u4eceAPI\u7528\u6237\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u201d\u8d44\u6e90\u201c\u5e94\u8be5\u662f\u4e2a\u540d\u8bcd\u3002\u5373\u4f7f\u4f60\u7684\u5185\u90e8\u6570\u636e\u6a21\u578b\u548c\u8d44\u6e90\u5df2\u7ecf\u6709\u4e86\u5f88\u597d\u7684\u5bf9\u5e94\uff0cAPI\u8bbe\u8ba1\u7684\u65f6\u5019\u4f60\u4ecd\u7136\u4e0d\u9700\u8981\u628a\u5b83\u4eec\u4e00\u5bf9\u4e00\u7684\u90fd\u66b4\u9732\u51fa\u6765\u3002\u8fd9\u91cc\u7684\u5173\u952e\u662f\u9690\u85cf\u5185\u90e8\u8d44\u6e90\uff0c\u66b4\u9732\u5fc5\u9700\u7684\u5916\u90e8\u8d44\u6e90\u3002<\/p>\n<p>\u5728SupportFu\u91cc\uff0c\u8d44\u6e90\u662f ticket\u3001user\u3001group\u3002<\/p>\n<p>\u4e00\u65e6\u5b9a\u4e49\u597d\u4e86\u8981\u66b4\u9732\u7684\u8d44\u6e90\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u8d44\u6e90\u4e0a\u5141\u8bb8\u7684\u64cd\u4f5c\uff0c\u4ee5\u53ca\u8fd9\u4e9b\u64cd\u4f5c\u548c\u4f60\u7684API\u7684\u5bf9\u5e94\u5173\u7cfb\uff1a<\/p>\n<ul>\n<li>GET \/tickets # \u83b7\u53d6ticket\u5217\u8868<\/li>\n<li>GET \/tickets\/12 # \u67e5\u770b\u67d0\u4e2a\u5177\u4f53\u7684ticket<\/li>\n<li>POST \/tickets # \u65b0\u5efa\u4e00\u4e2aticket<\/li>\n<li>PUT \/tickets\/12 # \u66f4\u65b0ticket 12.<\/li>\n<li>DELETE \/tickets\/12 #\u5220\u9664ticekt 12<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u770b\u51fa\u4f7f\u7528REST\u7684\u597d\u5904\u5728\u4e8e\u53ef\u4ee5\u5145\u5206\u5229\u7528http\u7684\u5f3a\u5927\u5b9e\u73b0\u5bf9\u8d44\u6e90\u7684CURD\u529f\u80fd\u3002\u800c\u8fd9\u91cc\u4f60\u53ea\u9700\u8981\u4e00\u4e2aendpoint\uff1a\/tickets,\u518d\u6ca1\u6709\u5176\u4ed6\u4ec0\u4e48\u547d\u540d\u89c4\u5219\u548curl\u89c4\u5219\u4e86\uff0ccool\uff01<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8fd9\u4e2aendpoint\u7684\u5355\u6570\u590d\u6570<\/strong><\/p>\n<p>\u4e00\u4e2a\u53ef\u4ee5\u9075\u4ece\u7684\u89c4\u5219\u662f\uff1a\u867d\u7136\u770b\u8d77\u6765\u4f7f\u7528\u590d\u6570\u6765\u63cf\u8ff0\u67d0\u4e00\u4e2a\u8d44\u6e90\u5b9e\u4f8b\u770b\u8d77\u6765\u522b\u626d\uff0c\u4f46\u662f\u7edf\u4e00\u6240\u6709\u7684endpoint\uff0c\u4f7f\u7528\u590d\u6570\u4f7f\u5f97\u4f60\u7684URL\u66f4\u52a0\u89c4\u6574\u3002\u8fd9\u8ba9API\u4f7f\u7528\u8005\u66f4\u52a0\u5bb9\u6613\u7406\u89e3\uff0c\u5bf9\u5f00\u53d1\u8005\u6765\u8bf4\u4e5f\u66f4\u5bb9\u6613\u5b9e\u73b0\u3002<\/p>\n<p>\u5982\u4f55\u5904\u7406\u5173\u8054\uff1f\u5173\u4e8e\u5982\u4f55\u5904\u7406\u8d44\u6e90\u4e4b\u95f4\u7684\u7ba1\u7406REST\u539f\u5219\u4e5f\u6709\u76f8\u5173\u7684\u63cf\u8ff0\uff1a<\/p>\n<ul>\n<li>GET \/tickets\/12\/messages- Retrieves list of messages for ticket #12<\/li>\n<li>GET \/tickets\/12\/messages\/5- Retrieves message #5 for ticket #12<\/li>\n<li>POST \/tickets\/12\/messages- Creates a new message in ticket #12<\/li>\n<li>PUT \/tickets\/12\/messages\/5- Updates message #5 for ticket #12<\/li>\n<li>PATCH \/tickets\/12\/messages\/5- Partially updates message #5 for ticket #12<\/li>\n<li>DELETE \/tickets\/12\/messages\/5- Deletes message #5 for ticket #12<\/li>\n<\/ul>\n<p>\u5176\u4e2d\uff0c\u5982\u679c\u8fd9\u79cd\u5173\u8054\u548c\u8d44\u6e90\u72ec\u7acb\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728\u8d44\u6e90\u7684\u8f93\u51fa\u8868\u793a\u4e2d\u4fdd\u5b58\u76f8\u5e94\u8d44\u6e90\u7684endpoint\u3002\u7136\u540eAPI\u7684\u4f7f\u7528\u8005\u5c31\u53ef\u4ee5\u901a\u8fc7\u70b9\u51fb\u94fe\u63a5\u627e\u5230\u76f8\u5173\u7684\u8d44\u6e90\u3002\u5982\u679c\u5173\u8054\u548c\u8d44\u6e90\u8054\u7cfb\u7d27\u5bc6\u3002\u8d44\u6e90\u7684\u8f93\u51fa\u8868\u793a\u5c31\u5e94\u8be5\u76f4\u63a5\u4fdd\u5b58\u76f8\u5e94\u8d44\u6e90\u4fe1\u606f\u3002\uff08\u4f8b\u5982\u8fd9\u91cc\u5982\u679cmessage\u8d44\u6e90\u662f\u72ec\u7acb\u5b58\u5728\u7684\uff0c\u90a3\u4e48\u4e0a\u9762 GET \/tickets\/12\/messages\u5c31\u4f1a\u8fd4\u56de\u76f8\u5e94message\u7684\u94fe\u63a5\uff1b\u76f8\u53cd\u7684\u5982\u679cmessage\u4e0d\u72ec\u7acb\u5b58\u5728\uff0c\u4ed6\u548cticket\u4f9d\u9644\u5b58\u5728\uff0c\u5219\u4e0a\u9762\u7684API\u8c03\u7528\u8fd4\u56de\u76f4\u63a5\u8fd4\u56demessage\u4fe1\u606f\uff09<\/p>\n<p><strong>\u4e0d\u7b26\u5408CURD\u7684\u64cd\u4f5c<\/strong><\/p>\n<p>\u5bf9\u8fd9\u4e2a\u4ee4\u4eba\u56f0\u60d1\u7684\u95ee\u9898\uff0c\u4e0b\u9762\u662f\u4e00\u4e9b\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u91cd\u6784\u4f60\u7684\u884c\u4e3aaction\u3002\u5f53\u4f60\u7684\u884c\u4e3a\u4e0d\u9700\u8981\u53c2\u6570\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u628aactive\u5bf9\u5e94\u5230activated\u8fd9\u4e2a\u8d44\u6e90\uff0c\uff08\u66f4\u65b0\u4f7f\u7528patch\uff09.<\/li>\n<li>\u4ee5\u5b50\u8d44\u6e90\u5bf9\u5f85\u3002\u4f8b\u5982:github\u4e0a\uff0c\u5bf9\u4e00\u4e2agists\u52a0\u661f\u64cd\u4f5c\uff1aPUT \/gists\/:id\/star \u5e76\u4e14\u53d6\u6d88\u661f\u64cd\u4f5c\uff1aDELETE \/gists\/:id\/star.<\/li>\n<li>\u6709\u65f6\u5019action\u5b9e\u5728\u6ca1\u6709\u96be\u4ee5\u548c\u67d0\u4e2a\u8d44\u6e90\u5bf9\u5e94\u4e0a\u4f8b\u5982search\u3002\u90a3\u5c31\u8fd9\u4e48\u529e\u5427\u3002\u6211\u8ba4\u4e3aAPI\u7684\u4f7f\u7528\u8005\u5bf9\u4e8e\/search\u8fd9\u79cdurl\u4e5f\u4e0d\u4f1a\u6709\u592a\u5927\u610f\u89c1\u7684\uff08\u6bd5\u7adf\u4ed6\u5f88\u5bb9\u6613\u7406\u89e3\uff09\u3002\u53ea\u8981\u6ce8\u610f\u5728\u6587\u6863\u4e2d\u5199\u6e05\u695a\u5c31\u53ef\u4ee5\u4e86\u3002<\/li>\n<li><\/li>\n<\/ol>\n<p><strong>\u6c38\u8fdc\u4f7f\u7528SSL<\/strong><\/p>\n<p>\u6beb\u65e0\u4f8b\u5916\uff0c\u6c38\u8fdc\u90fd\u8981\u4f7f\u7528SSL\u3002\u4f60\u7684\u5e94\u7528\u4e0d\u77e5\u9053\u8981\u88ab\u8c01\uff0c\u4ee5\u53ca\u4ec0\u4e48\u60c5\u51b5\u8bbf\u95ee\u3002\u6709\u4e9b\u662f\u5b89\u5168\u7684\uff0c\u6709\u4e9b\u4e0d\u662f\u3002\u4f7f\u7528SSL\u53ef\u4ee5\u51cf\u5c11\u9274\u6743\u7684\u6210\u672c\uff1a\u4f60\u53ea\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u7684\u4ee4\u724c\uff08token\uff09\u5c31\u53ef\u4ee5\u9274\u6743\u4e86\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u8ba9\u7528\u6237\u5bf9\u6bcf\u6b21\u8bf7\u6c42\u7b7e\u540d\u3002<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff1a\u4e0d\u8981\u8ba9\u975eSSL\u7684url\u8bbf\u95ee\u91cd\u5b9a\u5411\u5230SSL\u7684url\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6587\u6863<\/strong><\/p>\n<p>\u6587\u6863\u548cAPI\u672c\u8eab\u4e00\u6837\u91cd\u8981\u3002\u6587\u6863\u5e94\u8be5\u5bb9\u6613\u627e\u5230\uff0c\u5e76\u4e14\u516c\u5f00\uff08\u628a\u5b83\u4eec\u85cf\u5230pdf\u91cc\u9762\u6216\u8005\u5b58\u5230\u9700\u8981\u767b\u5f55\u7684\u5730\u65b9\u90fd\u4e0d\u592a\u597d\uff09\u3002\u6587\u6863\u5e94\u8be5\u6709\u5c55\u793a\u8bf7\u6c42\u548c\u8f93\u51fa\u7684\u4f8b\u5b50\uff1a\u6216\u8005\u4ee5\u70b9\u51fb\u94fe\u63a5\u7684\u65b9\u5f0f\u6216\u8005\u901a\u8fc7curl\u7684\u65b9\u5f0f\uff08\u8bf7\u89c1openstack\u7684\u6587\u6863\uff09\u3002\u5982\u679c\u6709\u66f4\u65b0\uff08\u7279\u522b\u662f\u516c\u5f00\u7684API\uff09\uff0c\u5e94\u8be5\u53ca\u65f6\u66f4\u65b0\u6587\u6863\u3002\u6587\u6863\u4e2d\u5e94\u8be5\u6709\u5173\u4e8e\u4f55\u65f6\u5f03\u7528\u67d0\u4e2aAPI\u7684\u65f6\u95f4\u8868\u4ee5\u53ca\u8be6\u60c5\u3002\u4f7f\u7528\u90ae\u4ef6\u5217\u8868\u6216\u8005\u535a\u5ba2\u8bb0\u5f55\u662f\u597d\u65b9\u6cd5\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u7248\u672c\u5316<\/strong><\/p>\n<p>\u5728API\u4e0a\u52a0\u5165\u7248\u672c\u4fe1\u606f\u53ef\u4ee5\u6709\u6548\u7684\u9632\u6b62\u7528\u6237\u8bbf\u95ee\u5df2\u7ecf\u66f4\u65b0\u4e86\u7684API\uff0c\u540c\u65f6\u4e5f\u80fd\u8ba9\u4e0d\u540c\u4e3b\u8981\u7248\u672c\u4e4b\u95f4\u5e73\u7a33\u8fc7\u6e21\u3002\u5173\u4e8e\u662f\u5426\u5c06\u7248\u672c\u4fe1\u606f\u653e\u5165url\u8fd8\u662f\u653e\u5165\u8bf7\u6c42\u5934\u6709\u8fc7\u4e89\u8bba\uff1a<a href=\"http:\/\/stackoverflow.com\/questions\/389169\/best-practices-for-API-versioning\">API version should be included in the URL or in a header<\/a>. \u5b66\u672f\u754c\u8bf4\u5b83\u5e94\u8be5\u653e\u5230header\u91cc\u9762\u53bb\uff0c\u4f46\u662f\u5982\u679c\u653e\u5230url\u91cc\u9762\u6211\u4eec\u5c31\u53ef\u4ee5\u8de8\u7248\u672c\u7684\u8bbf\u95ee\u8d44\u6e90\u4e86\u3002\u3002\uff08\u53c2\u8003openstack\uff09\u3002<\/p>\n<p>strip\u4f7f\u7528\u7684\u65b9\u6cd5\u5c31\u5f88\u597d\uff1a\u5b83\u7684url\u91cc\u9762\u6709\u4e3b\u7248\u672c\u4fe1\u606f\uff0c\u540c\u65f6\u8bf7\u6c42\u5934\u4fe9\u9762\u6709\u5b50\u7248\u672c\u4fe1\u606f\u3002\u8fd9\u6837\u5728\u5b50\u7248\u672c\u53d8\u5316\u8fc7\u7a0b\u4e2durl\u7684\u7a33\u5b9a\u7684\u3002\u53d8\u5316\u6709\u65f6\u662f\u4e0d\u53ef\u907f\u514d\u7684\uff0c\u5173\u952e\u662f\u5982\u4f55\u7ba1\u7406\u53d8\u5316\u3002\u5b8c\u6574\u7684\u6587\u6863\u548c\u5408\u7406\u7684\u65f6\u95f4\u8868\u90fd\u4f1a\u4f7f\u5f97API\u4f7f\u7528\u8005\u4f7f\u7528\u7684\u66f4\u52a0\u8f7b\u677e\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u7ed3\u679c\u8fc7\u6ee4\uff0c\u6392\u5e8f\uff0c\u641c\u7d22\uff1a<\/strong><\/p>\n<p>url\u6700\u597d\u8d8a\u7b80\u77ed\u8d8a\u597d\uff0c\u548c\u7ed3\u679c\u8fc7\u6ee4\uff0c\u6392\u5e8f\uff0c\u641c\u7d22\u76f8\u5173\u7684\u529f\u80fd\u90fd\u5e94\u8be5\u901a\u8fc7\u53c2\u6570\u5b9e\u73b0(\u5e76\u4e14\u4e5f\u5f88\u5bb9\u6613\u5b9e\u73b0)\u3002<\/p>\n<p><strong>\u8fc7\u6ee4\uff1a<\/strong>\u4e3a\u6240\u6709\u63d0\u4f9b\u8fc7\u6ee4\u529f\u80fd\u7684\u63a5\u53e3\u63d0\u4f9b\u7edf\u4e00\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff1a\u4f60\u60f3\u9650\u5236get \/tickets \u7684\u8fd4\u56de\u7ed3\u679c:\u53ea\u8fd4\u56de\u90a3\u4e9bopen\u72b6\u6001\u7684ticket\u2013get \/tickektsstate=open\u8fd9\u91cc\u7684state\u5c31\u662f\u8fc7\u6ee4\u53c2\u6570\u3002<\/p>\n<p><strong>\u6392\u5e8f\uff1a<\/strong>\u548c\u8fc7\u6ee4\u4e00\u6837\uff0c\u4e00\u4e2a\u597d\u7684\u6392\u5e8f\u53c2\u6570\u5e94\u8be5\u80fd\u591f\u63cf\u8ff0\u6392\u5e8f\u89c4\u5219\uff0c\u800c\u4e0d\u4e1a\u52a1\u76f8\u5173\u3002\u590d\u6742\u7684\u6392\u5e8f\u89c4\u5219\u5e94\u8be5\u901a\u8fc7\u7ec4\u5408\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li>GET \/ticketssort=-priority- Retrieves a list of tickets in descending order of priority<\/li>\n<li>GET \/ticketssort=-priority,created_at- Retrieves a list of tickets in descending order of priority. Within a specific priority, older tickets are ordered first<\/li>\n<\/ul>\n<p>\u8fd9\u91cc\u7b2c\u4e8c\u6761\u67e5\u8be2\u4e2d\uff0c\u6392\u5e8f\u89c4\u5219\u6709\u591a\u4e2arule\u4ee5\u9017\u53f7\u95f4\u9694\u7ec4\u5408\u800c\u6210\u3002<\/p>\n<p><strong>\u641c\u7d22\uff1a<\/strong>\u6709\u4e9b\u65f6\u5019\u7b80\u5355\u7684\u6392\u5e8f\u662f\u4e0d\u591f\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u641c\u7d22\u6280\u672f\uff08ElasticSearch\u548cLucene\uff09\u6765\u5b9e\u73b0\uff08\u4f9d\u65e7\u53ef\u4ee5\u4f5c\u4e3aurl\u7684\u53c2\u6570\uff09\u3002<\/p>\n<ul>\n<li>GET \/ticketsq=return&amp;state=open&amp;sort=-priority,created_at- Retrieve the highest priority open tickets mentioning the word \u2018return\u2019<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u7ecf\u5e38\u4f7f\u7528\u7684\u641c\u7d22\u67e5\u8be2\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u4ed6\u4eec\u8bbe\u7acb\u522b\u540d,\u8fd9\u6837\u4f1a\u8ba9API\u66f4\u52a0\u4f18\u96c5\u3002\u4f8b\u5982\uff1a<br \/>\nget \/ticketsq=recently_closed -&gt; get \/tickets\/recently_closed.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u9650\u5236API\u8fd4\u56de\u503c\u7684\u57df<\/strong><\/p>\n<p>\u6709\u65f6\u5019API\u4f7f\u7528\u8005\u4e0d\u9700\u8981\u6240\u6709\u7684\u7ed3\u679c\uff0c\u5728\u8fdb\u884c\u6a2a\u5411\u9650\u5236\u7684\u65f6\u5019\uff08\u4f8b\u5982\u503c\u8fd4\u56deAPI\u7ed3\u679c\u7684\u524d\u5341\u9879\uff09\u8fd8\u5e94\u8be5\u53ef\u4ee5\u8fdb\u884c\u7eb5\u5411\u9650\u5236\u3002\u5e76\u4e14\u8fd9\u4e2a\u529f\u80fd\u80fd\u6709\u6548\u7684\u63d0\u9ad8\u7f51\u7edc\u5e26\u5bbd\u4f7f\u7528\u7387\u548c\u901f\u5ea6\u3002\u53ef\u4ee5\u4f7f\u7528fields\u67e5\u8be2\u53c2\u6570\u6765\u9650\u5236\u8fd4\u56de\u7684\u57df\u4f8b\u5982\uff1a<br \/>\nGET \/ticketsfields=id,subject,customer_name,updated_at&amp;state=open&amp;sort=-updated_at<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u66f4\u65b0\u548c\u521b\u5efa\u64cd\u4f5c\u5e94\u8be5\u8fd4\u56de\u8d44\u6e90<\/strong><\/p>\n<p>PUT\u3001POST\u3001PATCH \u64cd\u4f5c\u5728\u5bf9\u8d44\u6e90\u8fdb\u884c\u64cd\u4f5c\u7684\u65f6\u5019\u5e38\u5e38\u6709\u4e00\u4e9b\u526f\u4f5c\u7528\uff1a\u4f8b\u5982created_at,updated_at \u65f6\u95f4\u6233\u3002\u4e3a\u4e86\u9632\u6b62\u7528\u6237\u591a\u6b21\u7684API\u8c03\u7528\uff08\u4e3a\u4e86\u8fdb\u884c\u6b64\u6b21\u7684\u66f4\u65b0\u64cd\u4f5c\uff09\uff0c\u6211\u4eec\u5e94\u8be5\u4f1a\u8fd4\u56de\u66f4\u65b0\u7684\u8d44\u6e90\uff08updated representation.\uff09\u4f8b\u5982\uff1a\u5728POST\u64cd\u4f5c\u4ee5\u540e\uff0c\u8fd4\u56de201 created \u72b6\u6001\u7801\uff0c\u5e76\u4e14\u5305\u542b\u4e00\u4e2a\u6307\u5411\u65b0\u8d44\u6e90\u7684url\u4f5c\u4e3a\u8fd4\u56de\u5934<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u662f\u5426\u9700\u8981 \u201cHATEOAS<\/strong>\u201c<\/p>\n<p>\u7f51\u4e0a\u5173\u4e8e\u662f\u5426\u5141\u8bb8\u7528\u6237\u521b\u5efa\u65b0\u7684url\u6709\u5f88\u5927\u7684\u5f02\u8bae\uff08\u6ce8\u610f\u4e0d\u662f\u521b\u5efa\u8d44\u6e90\u4ea7\u751f\u7684url\uff09\u3002\u4e3a\u6b64REST\u5236\u5b9a\u4e86HATEOAS\u6765\u63cf\u8ff0\u4e86\u548cendpoint\u8fdb\u884c\u4ea4\u4e92\u7684\u65f6\u5019\uff0c\u884c\u4e3a\u5e94\u8be5\u5728\u8d44\u6e90\u7684metadata\u8fd4\u56de\u503c\u91cc\u9762\u8fdb\u884c\u5b9a\u4e49\u3002<\/p>\n<p>\uff08\u8bd1\u6ce8\uff1a\u4f5c\u8005\u8fd9\u91cc\u8ba4\u4e3aHATEOAS\u8fd8\u4e0d\u7b97\u6210\u719f\uff0c\u6211\u4e5f\u4e0d\u600e\u4e48\u7406\u89e3\u8fd9\u6bb5\u5c31\u7b97\u4e86\uff0c\u8bfb\u8005\u611f\u5174\u8da3\u53ef\u4ee5\u81ea\u5df1\u53bb\u539f\u6587\u67e5\u770b\uff09<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u53ea\u63d0\u4f9bjson\u4f5c\u4e3a\u8fd4\u56de\u683c\u5f0f<\/strong><\/p>\n<p>\u73b0\u5728\u5f00\u59cb\u6bd4\u8f83\u4e00\u4e0bXML\u548cjson\u4e86\u3002XML\u5373\u5197\u957f\uff0c\u96be\u4ee5\u9605\u8bfb\uff0c\u53c8\u4e0d\u9002\u5408\u5404\u79cd\u7f16\u7a0b\u8bed\u8a00\u89e3\u6790\u3002\u5f53\u7136XML\u6709\u6269\u5c55\u6027\u7684\u4f18\u52bf\uff0c\u4f46\u662f\u5982\u679c\u4f60\u53ea\u662f\u5c06\u5b83\u6765\u5bf9\u5185\u90e8\u8d44\u6e90\u4e32\u884c\u5316\uff0c\u90a3\u4e48\u4ed6\u7684\u6269\u5c55\u4f18\u52bf\u4e5f\u53d1\u6325\u4e0d\u51fa\u6765\u3002\u5f88\u591a\u5e94\u7528\uff08youtube,twitter,box\uff09\u90fd\u5df2\u7ecf\u5f00\u59cb\u629b\u5f03XML\u4e86\uff0c\u6211\u4e5f\u4e0d\u60f3\u591a\u8d39\u53e3\u820c\u3002\u7ed9\u4e86google\u4e0a\u7684\u8d8b\u52bf\u56fe\u5427\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.vinaysahni.com\/images\/201305-xml-vs-json-api.png\" alt=\"\" \/><\/p>\n<p>\u5f53\u7136\u5982\u679c\u7684\u4f60\u4f7f\u7528\u7528\u6237\u91cc\u9762\u4f01\u4e1a\u7528\u6237\u5c45\u591a\uff0c\u90a3\u4e48\u53ef\u80fd\u9700\u8981\u652f\u6301XML\u3002\u5982\u679c\u662f\u8fd9\u6837\u7684\u8bdd\u4f60\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u95ee\u9898\uff1a\u4f60\u7684http\u8bf7\u6c42\u4e2d\u7684media\u7c7b\u578b\u662f\u5e94\u8be5\u548caccept \u5934\u540c\u6b65\u8fd8\u662f\u548curl\uff1f\u4e3a\u4e86\u65b9\u4fbf\uff08browser explorability\uff09,\u5e94\u8be5\u662f\u5728url\u4e2d(\u7528\u6237\u53ea\u8981\u81ea\u5df1\u62fcurl\u5c31\u597d\u4e86)\u3002\u5982\u679c\u8fd9\u6837\u7684\u8bdd\u6700\u597d\u7684\u65b9\u6cd5\u662f\u4f7f\u7528.xml\u6216\u8005.json\u7684\u540e\u7f00\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u547d\u540d\u65b9\u5f0f\uff1f<\/strong><\/p>\n<p>\u662f\u86c7\u5f62\u547d\u4ee4\uff08\u4e0b\u5212\u7ebf\u548c\u5c0f\u5199\uff09\u8fd8\u662f\u9a7c\u5cf0\u547d\u540d\uff1f\u5982\u679c\u4f7f\u7528json\u90a3\u4e48\u6700\u597d\u7684\u5e94\u8be5\u662f\u9075\u5b88JAVASCRIPT\u7684\u547d\u540d\u65b9\u6cd5-\u4e5f\u5c31\u662f\u8bf4\u9a86\u9a7c\u547d\u540d\u6cd5\u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u591a\u79cd\u8bed\u8a00\u5199\u4e00\u4e2a\u5e93\uff0c\u90a3\u4e48\u6700\u597d\u6309\u7167\u90a3\u4e9b\u8bed\u8a00\u6240\u63a8\u8350\u7684\uff0cjava\uff0cc#\u4f7f\u7528\u9a86\u9a7c\uff0cpython\uff0cruby\u4f7f\u7528snake\u3002<\/p>\n<p>\u4e2a\u4eba\u610f\u89c1\uff1a\u6211\u603b\u89c9\u5f97\u86c7\u5f62\u547d\u4ee4\u66f4\u597d\u4f7f\u4e00\u4e9b\uff0c\u5f53\u7136\u8fd9\u6ca1\u6709\u4ec0\u4e48\u7406\u8bba\u7684\u4f9d\u636e\u3002\u6709\u4eba\u8bf4\u86c7\u5f62\u547d\u540d\u8bfb\u8d77\u6765\u66f4\u5feb\uff0c\u80fd\u8fbe\u523020%\uff0c\u4e5f\u4e0d\u77e5\u9053\u771f\u5047<a href=\"http:\/\/ieeexplore.ieee.org\/xpl\/articleDetails.jsptp=&amp;arnumber=5521745\">http:\/\/ieeexplore.ieee.org\/xpl\/articleDetails.jsptp=&amp;arnumber=5521745<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u9ed8\u8ba4\u4f7f\u7528pretty print\u683c\u5f0f\uff0c\u4f7f\u7528gzip<\/strong><\/p>\n<p>\u53ea\u662f\u4f7f\u7528\u7a7a\u683c\u7684\u8fd4\u56de\u7ed3\u679c\u4ece\u6d4f\u89c8\u5668\u4e0a\u770b\u603b\u662f\u89c9\u5f97\u5f88\u6076\u5fc3(\u4e00\u5927\u5768\u6709\u6ca1\u6709\uff1f\uff5e)\u3002\u5f53\u7136\u4f60\u53ef\u4ee5\u63d0\u4f9burl\u4e0a\u7684\u53c2\u6570\u6765\u63a7\u5236\u4f7f\u7528\u201cpretty print\u201d\uff0c\u4f46\u662f\u9ed8\u8ba4\u5f00\u542f\u8fd9\u4e2a\u9009\u9879\u8fd8\u662f\u66f4\u52a0\u53cb\u597d\u3002\u683c\u5916\u7684\u4f20\u8f93\u4e0a\u7684\u635f\u5931\u4e0d\u4f1a\u592a\u5927\u3002\u76f8\u53cd\u4f60\u5982\u679c\u5fd8\u4e86\u4f7f\u7528gzip\u90a3\u4e48\u4f20\u8f93\u6548\u7387\u5c06\u4f1a\u5927\u5927\u51cf\u5c11\uff0c\u635f\u5931\u5927\u5927\u589e\u52a0\u3002\u60f3\u8c61\u4e00\u4e2a\u7528\u6237\u6b63\u5728debug\u90a3\u4e48\u9ed8\u8ba4\u7684\u8f93\u51fa\u5c31\u662f\u53ef\u8bfb\u7684-\u800c\u4e0d\u7528\u5c06\u7ed3\u679c\u62f7\u8d1d\u5230\u5176\u4ed6\u4ec0\u4e48\u8f6f\u4ef6\u4e2d\u5728\u683c\u5f0f\u5316-\u662f\u60f3\u8d77\u6765\u5c31\u5f88\u723d\u7684\u4e8b\uff0c\u4e0d\u662f\u4e48\uff1f<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50:<\/p>\n<pre>$ curl https:\/\/API.github.com\/users\/veesahni &gt; with-whitespace.txt\r\n$ ruby -r json -e 'puts JSON JSON.parse(STDIN.read)' &lt; with-whitespace.txt &gt; without-whitespace.txt\r\n$ gzip -c with-whitespace.txt &gt; with-whitespace.txt.gz\r\n$ gzip -c without-whitespace.txt &gt; without-whitespace.txt.gz<\/pre>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>without-whitespace.txt- 1252 bytes<\/li>\n<li>with-whitespace.txt- 1369 bytes<\/li>\n<li>without-whitespace.txt.gz- 496 bytes<\/li>\n<li>with-whitespace.txt.gz- 509 bytes<\/li>\n<\/ul>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u591a\u4f59\u7684\u7a7a\u683c\u4f7f\u5f97\u7ed3\u679c\u5927\u5c0f\u591a\u51fa\u4e868.5%\uff08\u6ca1\u6709\u4f7f\u7528gzip\uff09\uff0c\u76f8\u53cd\u53ea\u591a\u51fa\u4e862.6%\u3002\u636e\u8bf4\uff1atwitter\u4f7f\u7528gzip\u4e4b\u540e\u5b83\u7684streaming API\u4f20\u8f93\u51cf\u5c11\u4e8680%\uff08link:https:\/\/dev.twitter.com\/blog\/announcing-gzip-compression-streaming-APIs\uff09.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u53ea\u5728\u9700\u8981\u7684\u65f6\u5019\u4f7f\u7528\u201cenvelope\u201d<\/strong><\/p>\n<p>\u5f88\u591aAPI\u8c61\u4e0b\u9762\u8fd9\u6837\u8fd4\u56de\u7ed3\u679c\uff1a<\/p>\n<div>\n<div id=\"highlighter_769720\" class=\"syntaxhighlighter notranslate c\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"data\"<\/code> <code class=\"c plain\">: {<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"id\"<\/code> <code class=\"c plain\">: 123,<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"name\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"John\"<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c plain\">}<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"c plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u7406\u7531\u5f88\u7b80\u5355\uff1a\u8fd9\u6837\u505a\u53ef\u4ee5\u5f88\u5bb9\u6613\u6269\u5c55\u8fd4\u56de\u7ed3\u679c\uff0c\u4f60\u53ef\u4ee5\u52a0\u5165\u4e00\u4e9b\u5206\u9875\u4fe1\u606f\uff0c\u4e00\u4e9b\u6570\u636e\u7684\u5143\u4fe1\u606f\u7b49\uff0d\u8fd9\u5bf9\u4e8e\u90a3\u4e9b\u4e0d\u5bb9\u6613\u8bbf\u95ee\u5230\u8fd4\u56de\u5934\u7684API\u4f7f\u7528\u8005\u6765\u8bf4\u786e\u5b9e\u6709\u7528\uff0c\u4f46\u662f\u968f\u7740\u201c\u6807\u51c6\u201d\u7684\u53d1\u5c55\uff08cors\u548chttp:\/\/tools.ietf.org\/html\/rfc5988#page-6\u90fd\u5f00\u59cb\u88ab\u52a0\u5165\u5230\u6807\u51c6\u4e2d\u4e86\uff09\uff0c\u6211\u4e2a\u4eba\u63a8\u8350\u4e0d\u8981\u90a3\u4e48\u505a\u3002<\/p>\n<p><strong>\u4f55\u65f6\u4f7f\u7528envelope\uff1f<\/strong><\/p>\n<p>\u6709\u4e24\u79cd\u60c5\u51b5\u662f\u5e94\u8be5\u4f7f\u7528envelope\u7684\u3002\u5982\u679cAPI\u4f7f\u7528\u8005\u786e\u5b9e\u65e0\u6cd5\u8bbf\u95ee\u8fd4\u56de\u5934\uff0c\u6216\u8005API\u9700\u8981\u652f\u6301\u4ea4\u53c9\u57df\u8bf7\u6c42\uff08\u901a\u8fc7jsonp\uff09\u3002<br \/>\njsonp\u8bf7\u6c42\u5728\u8bf7\u6c42\u7684url\u4e2d\u5305\u542b\u4e86\u4e00\u4e2acallback\u51fd\u6570\u53c2\u6570\u3002\u5982\u679c\u7ed9\u51fa\u4e86\u8fd9\u4e2a\u53c2\u6570\uff0c\u90a3\u4e48API\u5e94\u8be5\u8fd4\u56de200\uff0c\u5e76\u4e14\u628a\u771f\u6b63\u7684\u72b6\u6001\u7801\u653e\u5230\u8fd4\u56de\u503c\u91cc\u9762\uff08\u5305\u88c5\u5728\u4fe1\u5c01\u91cc\uff09\uff0c\u4f8b\u5982\uff1a<\/p>\n<div>\n<div id=\"highlighter_986489\" class=\"syntaxhighlighter notranslate javascript\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"javascript plain\">callback_function({<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"javascript spaces\">\u00a0\u00a0<\/code><code class=\"javascript plain\">status_code: 200,<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"javascript spaces\">\u00a0\u00a0<\/code><code class=\"javascript plain\">next_page: <\/code><code class=\"javascript string\">\"https:\/\/..\"<\/code><code class=\"javascript plain\">,<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"javascript spaces\">\u00a0\u00a0<\/code><code class=\"javascript plain\">response: {<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"javascript spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"javascript plain\">... actual JSON response body ... <\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"javascript spaces\">\u00a0\u00a0<\/code><code class=\"javascript plain\">}<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"javascript plain\">})<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u540c\u6837\u4e3a\u4e86\u652f\u6301\u65e0\u6cd5\u65b9\u6cd5\u8fd4\u56de\u5934\u7684API\u4f7f\u7528\u8005\uff0c\u53ef\u4ee5\u5141\u8bb8envelope=true\u8fd9\u6837\u7684\u53c2\u6570\u3002<\/p>\n<p><strong>\u5728post,put,patch\u4e0a\u4f7f\u7528json\u4f5c\u4e3a\u8f93\u5165<\/strong><\/p>\n<p>\u5982\u679c\u4f60\u8ba4\u540c\u6211\u4e0a\u9762\u8bf4\u7684\uff0c\u90a3\u4e48\u4f60\u5e94\u8be5\u51b3\u5b9a\u4f7f\u7528json\u4f5c\u4e3a\u6240\u6709\u7684API\u8f93\u51fa\u683c\u5f0f\uff0c\u90a3\u4e48\u6211\u4eec\u63a5\u4e0b\u6765\u8003\u8651\u8003\u8651API\u7684\u8f93\u5165\u6570\u636e\u683c\u5f0f\u3002<br \/>\n\u5f88\u591a\u7684API\u4f7f\u7528url\u7f16\u7801\u683c\u5f0f\uff1a\u5c31\u50cf\u662furl\u67e5\u8be2\u53c2\u6570\u7684\u683c\u5f0f\u4e00\u6837\uff1a\u5355\u7eaf\u7684\u952e\u503c\u5bf9\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u4f46\u662f\u4e5f\u6709\u81ea\u5df1\u7684\u95ee\u9898\uff1a\u5b83\u6ca1\u6709\u6570\u636e\u7c7b\u578b\u7684\u6982\u5ff5\u3002\u8fd9\u4f7f\u5f97\u7a0b\u5e8f\u4e0d\u5f97\u4e0d\u6839\u636e\u5b57\u7b26\u4e32\u89e3\u6790\u51fa\u5e03\u5c14\u548c\u6574\u6570,\u800c\u4e14\u8fd8\u6ca1\u6709\u5c42\u6b21\u7ed3\u6784\u2013\u867d\u7136\u6709\u4e00\u4e9b\u5173\u4e8e\u5c42\u6b21\u7ed3\u6784\u4fe1\u606f\u7684\u7ea6\u5b9a\u5b58\u5728\u53ef\u662f\u548c\u672c\u8eab\u5c31\u652f\u6301\u5c42\u6b21\u7ed3\u6784\u7684json\u6bd4\u8f83\u4e00\u4e0b\u8fd8\u662f\u4e0d\u5f88\u597d\u7528\u3002<\/p>\n<p>\u5f53\u7136\u5982\u679cAPI\u672c\u8eab\u5c31\u5f88\u7b80\u5355\uff0c\u90a3\u4e48\u4f7f\u7528url\u683c\u5f0f\u7684\u8f93\u5165\u6ca1\u4ec0\u4e48\u95ee\u9898\u3002\u4f46\u5bf9\u4e8e\u590d\u6742\u7684API\u4f60\u5e94\u8be5\u4f7f\u7528json\u3002\u6216\u8005\u5e72\u8106\u7edf\u4e00\u4f7f\u7528json\u3002<br \/>\n\u6ce8\u610f\u4f7f\u7528json\u4f20\u8f93\u7684\u65f6\u5019\uff0c\u8981\u6c42\u8bf7\u6c42\u5934\u91cc\u9762\u52a0\u5165\uff1aContent-Type\uff1aapplication\/json.\uff0c\u5426\u5219\u629b\u51fa415\u5f02\u5e38\uff08unsupported media type\uff09\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5206\u9875<\/strong><\/p>\n<p>\u5206\u9875\u6570\u636e\u53ef\u4ee5\u653e\u5230\u201c\u4fe1\u5c01\u201d\u91cc\u9762\uff0c\u4f46\u968f\u7740\u6807\u51c6\u7684\u6539\u8fdb\uff0c\u73b0\u5728\u6211\u63a8\u8350\u5c06\u5206\u9875\u4fe1\u606f\u653e\u5230link header\u91cc\u9762\uff1ahttp:\/\/tools.ietf.org\/html\/rfc5988#page-6\u3002<\/p>\n<p>\u4f7f\u7528link header\u7684API\u5e94\u8be5\u8fd4\u56de\u4e00\u7cfb\u5217\u7ec4\u5408\u597d\u4e86\u7684url\u800c\u4e0d\u662f\u8ba9\u7528\u6237\u81ea\u5df1\u518d\u53bb\u62fc\u3002\u8fd9\u70b9\u5728\u57fa\u4e8e\u6e38\u6807\u7684\u5206\u9875\u4e2d\u5c24\u4e3a\u91cd\u8981\u3002\u4f8b\u5982\u4e0b\u9762\uff0c\u6765\u81eagithub\u7684\u6587\u6863<\/p>\n<div>\n<div id=\"highlighter_459007\" class=\"syntaxhighlighter notranslate text\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"text plain\">Link: &lt;https:\/\/api.github.com\/user\/repos?page=3&amp;per_page=100&gt;; rel=\"next\", <\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"text plain\">&lt;https:\/\/api.github.com\/user\/repos?page=50&amp;per_page=100&gt;; rel=\"last\"<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>\u81ea\u52a8\u52a0\u8f7d\u76f8\u5173\u7684\u8d44\u6e90<\/strong><\/p>\n<p>\u5f88\u591a\u65f6\u5019\uff0c\u81ea\u52a8\u52a0\u8f7d\u76f8\u5173\u8d44\u6e90\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u5f88\u5927\u7684\u63d0\u9ad8\u6548\u7387\u3002\u4f46\u662f\u8fd9\u5374<a href=\"http:\/\/idbentley.com\/blog\/2013\/03\/14\/should-restful-apis-include-relationships\/\" target=\"_blank\">\u548cRESTful\u7684\u539f\u5219\u76f8\u80cc<\/a>\u3002\u4e3a\u4e86\u5982\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u5728url\u4e2d\u6dfb\u52a0\u53c2\u6570\uff1aembed\uff08\u6216\u8005expend\uff09\u3002embed\u53ef\u4ee5\u662f\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u4e32\uff0c\u4f8b\u5982\uff1a<\/p>\n<div>\n<div id=\"highlighter_34799\" class=\"syntaxhighlighter notranslate text\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"text plain\">GET \/ticket\/12embed=customer.name,assigned_user<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u5bf9\u5e94\u7684API\u8fd4\u56de\u503c\u5982\u4e0b\uff1a<\/p>\n<div>\n<div id=\"highlighter_390693\" class=\"syntaxhighlighter notranslate c\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"id\"<\/code> <code class=\"c plain\">: 12,<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"subject\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"I have a question!\"<\/code><code class=\"c plain\">,<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"summary\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"Hi, ....\"<\/code><code class=\"c plain\">,<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"customer\"<\/code> <code class=\"c plain\">: {<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"name\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"Bob\"<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c plain\">},<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c plain\">assigned_user: {<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"id\"<\/code> <code class=\"c plain\">: 42,<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"name\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"Jim\"<\/code><code class=\"c plain\">,<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c plain\">}<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"c plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u503c\u5f97\u63d0\u9192\u7684\u662f\uff0c\u8fd9\u4e2a\u529f\u80fd\u6709\u65f6\u5019\u4f1a\u5f88\u590d\u6742\uff0c\u5e76\u4e14\u53ef\u80fd\u5bfc\u81f4<a href=\"http:\/\/stackoverflow.com\/questions\/97197\/what-is-the-n1-selects-issue\" target=\"_blank\">N+1 SELECT \u95ee\u9898<\/a>\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u91cd\u5199HTTP\u65b9\u6cd5<\/strong><\/p>\n<p>\u6709\u7684\u5ba2\u6237\u7aef\u53ea\u80fd\u53d1\u51fa\u7b80\u5355\u7684GET \u548cPOST\u8bf7\u6c42\u3002\u4e3a\u4e86\u7167\u987e\u4ed6\u4eec\uff0c\u6211\u4eec\u53ef\u4ee5\u91cd\u5199HTTP\u8bf7\u6c42\u3002\u8fd9\u91cc\u6ca1\u6709\u4ec0\u4e48\u6807\u51c6\uff0c\u4f46\u662f\u4e00\u4e2a\u666e\u904d\u7684\u65b9\u5f0f\u662f\u63a5\u53d7X-HTTP-Method-Override\u8bf7\u6c42\u5934\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u901f\u5ea6\u9650\u5236<\/strong><\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8bf7\u6c42\u6cdb\u6ee5\uff0c\u7ed9API\u8bbe\u7f6e\u901f\u5ea6\u9650\u5236\u5f88\u91cd\u8981\u3002\u4e3a\u6b64\u00a0<a href=\"http:\/\/tools.ietf.org\/html\/rfc6585\">RFC 6585<\/a>\u00a0\u5f15\u5165\u4e86HTTP\u72b6\u6001\u7801<a href=\"http:\/\/tools.ietf.org\/html\/rfc6585#section-4\" target=\"_blank\">429\uff08too many requests\uff09<\/a>\u3002\u52a0\u5165\u901f\u5ea6\u8bbe\u7f6e\u4e4b\u540e\uff0c\u5e94\u8be5\u63d0\u793a\u7528\u6237\uff0c\u81f3\u4e8e\u5982\u4f55\u63d0\u793a\u6807\u51c6\u4e0a\u6ca1\u6709\u8bf4\u660e\uff0c\u4e0d\u8fc7\u6d41\u884c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528HTTP\u7684\u8fd4\u56de\u5934\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u51e0\u4e2a\u5fc5\u987b\u7684\u8fd4\u56de\u5934\uff08\u4f9d\u7167twitter\u7684\u547d\u540d\u89c4\u5219\uff09\uff1a<\/p>\n<ul>\n<li>X-Rate-Limit-Limit :\u5f53\u524d\u65f6\u95f4\u6bb5\u5141\u8bb8\u7684\u5e76\u53d1\u8bf7\u6c42\u6570<\/li>\n<li>X-Rate-Limit-Remaining:\u5f53\u524d\u65f6\u95f4\u6bb5\u4fdd\u7559\u7684\u8bf7\u6c42\u6570\u3002<\/li>\n<li>X-Rate-Limit-Reset:\u5f53\u524d\u65f6\u95f4\u6bb5\u5269\u4f59\u79d2\u6570<\/li>\n<\/ul>\n<p><strong>\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5f53\u524d\u65f6\u95f4\u6bb5\u5269\u4f59\u79d2\u6570\u800c\u4e0d\u662f\u65f6\u95f4\u6233\uff1f<\/strong><\/p>\n<p>\u65f6\u95f4\u6233\u4fdd\u5b58\u7684\u4fe1\u606f\u5f88\u591a\uff0c\u4f46\u662f\u4e5f\u5305\u542b\u4e86\u5f88\u591a\u4e0d\u5fc5\u8981\u7684\u4fe1\u606f\uff0c\u7528\u6237\u53ea\u9700\u8981\u77e5\u9053\u8fd8\u5269\u51e0\u79d2\u5c31\u53ef\u4ee5\u518d\u53d1\u8bf7\u6c42\u4e86\u8fd9\u6837\u4e5f\u907f\u514d\u4e86<a href=\"http:\/\/en.wikipedia.org\/wiki\/Clock_skew\" target=\"_blank\">clock skew\u95ee\u9898<\/a>\u3002<\/p>\n<p>\u6709\u4e9bAPI\u4f7f\u7528UNIX\u683c\u5f0f\u65f6\u95f4\u6233\uff0c\u6211\u5efa\u8bae\u4e0d\u8981\u90a3\u4e48\u5e72\u3002\u4e3a\u4ec0\u4e48\uff1fHTTP \u5df2\u7ecf\u89c4\u5b9a\u4e86\u4f7f\u7528 <a href=\"http:\/\/www.ietf.org\/rfc\/rfc1123.txt\">RFC 1123<\/a>\u00a0\u65f6\u95f4\u683c\u5f0f<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u9274\u6743\u00a0Authentication<\/strong><\/p>\n<p>restful API\u662f\u65e0\u72b6\u6001\u7684\u4e5f\u5c31\u662f\u8bf4\u7528\u6237\u8bf7\u6c42\u7684\u9274\u6743\u548ccookie\u4ee5\u53casession\u65e0\u5173\uff0c\u6bcf\u4e00\u6b21\u8bf7\u6c42\u90fd\u5e94\u8be5\u5305\u542b\u9274\u6743\u8bc1\u660e\u3002<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528ssl\u6211\u4eec\u53ef\u4ee5\u4e0d\u7528\u6bcf\u6b21\u90fd\u63d0\u4f9b\u7528\u6237\u540d\u548c\u5bc6\u7801\uff1a\u6211\u4eec\u53ef\u4ee5\u7ed9\u7528\u6237\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u4ea7\u751f\u7684token\u3002\u8fd9\u6837\u53ef\u4ee5\u6781\u5927\u7684\u65b9\u4fbf\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95eeAPI\u7684\u7528\u6237\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u7528\u6237\u53ef\u4ee5\u9996\u5148\u901a\u8fc7\u4e00\u6b21\u7528\u6237\u540d-\u5bc6\u7801\u7684\u9a8c\u8bc1\u5e76\u5f97\u5230token\uff0c\u5e76\u4e14\u53ef\u4ee5\u62f7\u8d1d\u8fd4\u56de\u7684token\u5230\u4ee5\u540e\u7684\u8bf7\u6c42\u4e2d\u3002\u5982\u679c\u4e0d\u65b9\u4fbf\uff0c\u53ef\u4ee5\u4f7f\u7528OAuth 2\u6765\u8fdb\u884ctoken\u7684\u5b89\u5168\u4f20\u8f93\u3002<\/p>\n<p>\u652f\u6301jsonp\u7684API\u9700\u8981\u989d\u5916\u7684\u9274\u6743\u65b9\u6cd5\uff0c\u56e0\u4e3ajsonp\u8bf7\u6c42\u65e0\u6cd5\u53d1\u9001\u666e\u901a\u7684credential\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5728\u67e5\u8be2url\u4e2d\u6dfb\u52a0\u53c2\u6570\uff1aaccess_token\u3002\u6ce8\u610f\u4f7f\u7528url\u53c2\u6570\u7684\u95ee\u9898\u662f\uff1a\u76ee\u524d\u5927\u90e8\u5206\u7684\u7f51\u7edc\u670d\u52a1\u5668\u90fd\u4f1a\u8bb2query\u53c2\u6570\u4fdd\u5b58\u5230\u670d\u52a1\u5668\u65e5\u5fd7\u4e2d\uff0c\u8fd9\u53ef\u80fd\u4f1a\u6210\u4e3a\u5927\u7684\u5b89\u5168\u98ce\u9669\u3002<\/p>\n<p>\u6ce8\u610f\u4e0a\u9762\u8bf4\u5230\u7684\u53ea\u662f\u4e09\u79cd\u4f20\u8f93token\u7684\u65b9\u6cd5\uff0c\u5b9e\u9645\u4f20\u8f93\u7684token\u53ef\u80fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u7f13\u5b58<\/strong><\/p>\n<p>HTTP\u63d0\u4f9b\u4e86\u81ea\u5e26\u7684\u7f13\u5b58\u6846\u67b6\u3002\u4f60\u9700\u8981\u505a\u7684\u662f\u5728\u8fd4\u56de\u7684\u65f6\u5019\u52a0\u5165\u4e00\u4e9b\u8fd4\u56de\u5934\u4fe1\u606f\uff0c\u5728\u63a5\u53d7\u8f93\u5165\u7684\u65f6\u5019\u52a0\u5165\u8f93\u5165\u9a8c\u8bc1\u3002\u57fa\u672c\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>ETag\uff1a<\/strong>\u5f53\u751f\u6210\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u5728HTTP\u5934\u91cc\u9762\u52a0\u5165ETag\uff0c\u5176\u4e2d\u5305\u542b\u8bf7\u6c42\u7684\u6821\u9a8c\u548c\u548c\u54c8\u5e0c\u503c\uff0c\u8fd9\u4e2a\u503c\u548c\u5728\u8f93\u5165\u53d8\u5316\u7684\u65f6\u5019\u4e5f\u5e94\u8be5\u53d8\u5316\u3002\u5982\u679c\u8f93\u5165\u7684HTTP\u8bf7\u6c42\u5305\u542bIF-NONE-MATCH\u5934\u4ee5\u53ca\u4e00\u4e2aETag\u503c\uff0c\u90a3\u4e48API\u5e94\u8be5\u8fd4\u56de304 not modified\u72b6\u6001\u7801\uff0c\u800c\u4e0d\u662f\u5e38\u89c4\u7684\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n<p><strong>Last-Modified\uff1a<\/strong>\u548cetag\u4e00\u6837\uff0c\u53ea\u662f\u591a\u4e86\u4e00\u4e2a\u65f6\u95f4\u6233\u3002\u8fd4\u56de\u5934\u91cc\u7684Last-Modified\uff1a\u5305\u542b\u4e86\u00a0<a href=\"http:\/\/www.ietf.org\/rfc\/rfc1123.txt\">RFC 1123<\/a>\u00a0\u65f6\u95f4\u6233\uff0c\u5b83\u548cIF-MODIFIED-SINCE\u4e00\u81f4\u3002HTTP\u89c4\u8303\u91cc\u9762\u6709\u4e09\u79cddate\u683c\u5f0f\uff0c\u670d\u52a1\u5668\u5e94\u8be5\u90fd\u80fd\u5904\u7406\u3002<\/p>\n<p><strong>\u51fa\u9519\u5904\u7406<\/strong><\/p>\n<p>\u5c31\u50cfhtml\u9519\u8bef\u9875\u9762\u80fd\u591f\u663e\u793a\u9519\u8bef\u4fe1\u606f\u4e00\u6837\uff0cAPI \u4e5f\u5e94\u8be5\u80fd\u8fd4\u56de\u53ef\u8bfb\u7684\u9519\u8bef\u4fe1\u606f\u2013\u5b83\u5e94\u8be5\u548c\u4e00\u822c\u7684\u8d44\u6e90\u683c\u5f0f\u4e00\u81f4\u3002API\u5e94\u8be5\u59cb\u7ec8\u8fd4\u56de\u76f8\u5e94\u7684\u72b6\u6001\u7801\uff0c\u4ee5\u53cd\u6620\u670d\u52a1\u5668\u6216\u8005\u8bf7\u6c42\u7684\u72b6\u6001\u3002API\u7684\u9519\u8bef\u7801\u53ef\u4ee5\u5206\u4e3a\u4e24\u90e8\u5206\uff0c400\u7cfb\u5217\u548c500\u7cfb\u5217\uff0c400\u7cfb\u5217\u8868\u660e\u5ba2\u6237\u7aef\u9519\u8bef\uff1a\u5982\u9519\u8bef\u7684\u8bf7\u6c42\u683c\u5f0f\u7b49\u3002500\u7cfb\u5217\u8868\u793a\u670d\u52a1\u5668\u9519\u8bef\u3002API\u5e94\u8be5\u81f3\u5c11\u5c06\u6240\u6709\u7684400\u7cfb\u5217\u7684\u9519\u8bef\u4ee5json\u5f62\u5f0f\u8fd4\u56de\u3002\u5982\u679c\u53ef\u80fd500\u7cfb\u5217\u7684\u9519\u8bef\u4e5f\u5e94\u8be5\u5982\u6b64\u3002json\u683c\u5f0f\u7684\u9519\u8bef\u5e94\u8be5\u5305\u542b\u4ee5\u4e0b\u4fe1\u606f\uff1a\u4e00\u4e2a\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4e00\u4e2a\u552f\u4e00\u7684\u9519\u8bef\u7801\uff0c\u4ee5\u53ca\u4efb\u4f55\u53ef\u80fd\u7684\u8be6\u7ec6\u9519\u8bef\u63cf\u8ff0\u3002\u5982\u4e0b\uff1a<\/p>\n<div>\n<div id=\"highlighter_78789\" class=\"syntaxhighlighter notranslate c\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"code\"<\/code> <code class=\"c plain\">: 1234,<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"message\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"Something bad happened :-(\"<\/code><code class=\"c plain\">,<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"description\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"More details about the error here\"<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"c plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u5bf9PUT,POST,PATCH\u7684\u8f93\u5165\u7684\u6821\u9a8c\u4e5f\u5e94\u8be5\u8fd4\u56de\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4f8b\u5982\uff1a<\/p>\n<div>\n<div id=\"highlighter_531222\" class=\"syntaxhighlighter notranslate c\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<div class=\"line number14 index13 alt1\">14<\/div>\n<div class=\"line number15 index14 alt2\">15<\/div>\n<div class=\"line number16 index15 alt1\">16<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"code\"<\/code> <code class=\"c plain\">: 1024,<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"message\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"Validation Failed\"<\/code><code class=\"c plain\">,<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c string\">\"errors\"<\/code> <code class=\"c plain\">: [<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"code\"<\/code> <code class=\"c plain\">: 5432,<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"field\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"first_name\"<\/code><code class=\"c plain\">,<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"message\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"First name cannot have fancy characters\"<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c plain\">},<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c plain\">{<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"code\"<\/code> <code class=\"c plain\">: 5622,<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"field\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"password\"<\/code><code class=\"c plain\">,<\/code><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c string\">\"message\"<\/code> <code class=\"c plain\">: <\/code><code class=\"c string\">\"Password cannot be blank\"<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"c spaces\">\u00a0\u00a0\u00a0\u00a0<\/code><code class=\"c plain\">}<\/code><\/div>\n<div class=\"line number15 index14 alt2\"><code class=\"c spaces\">\u00a0\u00a0<\/code><code class=\"c plain\">]<\/code><\/div>\n<div class=\"line number16 index15 alt1\"><code class=\"c plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>HTTP \u72b6\u6001\u7801<\/strong><\/p>\n<div>\n<div id=\"highlighter_908936\" class=\"syntaxhighlighter notranslate text\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"text plain\">200 ok \u00a0- \u6210\u529f\u8fd4\u56de\u72b6\u6001\uff0c\u5bf9\u5e94\uff0cGET,PUT,PATCH,DELETE.<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">201 created\u00a0\u00a0-\u00a0\u6210\u529f\u521b\u5efa\u3002<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">304 not modified \u00a0\u00a0-\u00a0HTTP\u7f13\u5b58\u6709\u6548\u3002<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">400 bad request \u00a0\u00a0-\u00a0\u8bf7\u6c42\u683c\u5f0f\u9519\u8bef\u3002<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">401 unauthorized \u00a0\u00a0-\u00a0\u672a\u6388\u6743\u3002<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">403 forbidden \u00a0\u00a0-\u00a0\u9274\u6743\u6210\u529f\uff0c\u4f46\u662f\u8be5\u7528\u6237\u6ca1\u6709\u6743\u9650\u3002<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">404 not found - \u8bf7\u6c42\u7684\u8d44\u6e90\u4e0d\u5b58\u5728<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">405 method not allowed - \u8be5http\u65b9\u6cd5\u4e0d\u88ab\u5141\u8bb8\u3002<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">410 gone - \u8fd9\u4e2aurl\u5bf9\u5e94\u7684\u8d44\u6e90\u73b0\u5728\u4e0d\u53ef\u7528\u3002<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">415 unsupported media type - \u8bf7\u6c42\u7c7b\u578b\u9519\u8bef\u3002<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">422 unprocessable entity - \u6821\u9a8c\u9519\u8bef\u65f6\u7528\u3002<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><code class=\"text spaces\">\u00a0<\/code><code class=\"text plain\">429 too many request - \u8bf7\u6c42\u8fc7\u591a\u3002<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90: http:\/\/blog.jobbole.com\/41233\/ RESTf &hellip; <a href=\"https:\/\/chen-hongyi.com\/?p=225\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[49],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-9","tag-restfulapi"],"_links":{"self":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225"}],"version-history":[{"count":2,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}