{"id":1121,"date":"2018-06-26T16:56:02","date_gmt":"2018-06-26T08:56:02","guid":{"rendered":"https:\/\/www.xiaobo.li\/?p=1121"},"modified":"2018-06-26T17:09:38","modified_gmt":"2018-06-26T09:09:38","slug":"api-http-www-ruanyifeng-com-blog-2014-05-restful_api-html","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/1121","title":{"rendered":"API \u8bbe\u8ba1\u6307\u5357"},"content":{"rendered":"<p>\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\uff0c\u5206\u4e3a\u524d\u7aef\u548c\u540e\u7aef\u4e24\u4e2a\u90e8\u5206\u3002\u5f53\u524d\u7684\u53d1\u5c55\u8d8b\u52bf\uff0c\u5c31\u662f\u524d\u7aef\u8bbe\u5907\u5c42\u51fa\u4e0d\u7a77\uff08\u624b\u673a\u3001\u5e73\u677f\u3001\u684c\u9762\u7535\u8111\u3001\u5176\u4ed6\u4e13\u7528\u8bbe\u5907......\uff09\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5fc5\u987b\u6709\u4e00\u79cd\u7edf\u4e00\u7684\u673a\u5236\uff0c\u65b9\u4fbf\u4e0d\u540c\u7684\u524d\u7aef\u8bbe\u5907\u4e0e\u540e\u7aef\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<p>\u8fd9\u5bfc\u81f4API\u6784\u67b6\u7684\u6d41\u884c\uff0c\u751a\u81f3\u51fa\u73b0<a href=\"http:\/\/www.google.com.hk\/search?q=API+first\" target=\"_blank\" rel=\"noopener\">\"API First\"<\/a>\u7684\u8bbe\u8ba1\u601d\u60f3\u3002<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Representational_state_transfer\" target=\"_blank\" rel=\"noopener\">RESTful API<\/a>\u662f\u76ee\u524d\u6bd4\u8f83\u6210\u719f\u7684\u4e00\u5957\u4e92\u8054\u7f51\u5e94\u7528\u7a0b\u5e8f\u7684API\u8bbe\u8ba1\u7406\u8bba\u3002<\/p>\n<p>\u6211\u4ee5\u524d\u5199\u8fc7\u4e00\u7bc7<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2011\/09\/restful.html\" target=\"_blank\" rel=\"noopener\">\u300a\u7406\u89e3RESTful\u67b6\u6784\u300b<\/a>\uff0c\u63a2\u8ba8\u5982\u4f55\u7406\u89e3\u8fd9\u4e2a\u6982\u5ff5\u3002<\/p>\n<p>\u4eca\u5929\uff0c\u6211\u5c06\u4ecb\u7ecdRESTful API\u7684\u8bbe\u8ba1\u7ec6\u8282\uff0c\u63a2\u8ba8\u5982\u4f55\u8bbe\u8ba1\u4e00\u5957\u5408\u7406\u3001\u597d\u7528\u7684API\u3002\u6211\u7684\u4e3b\u8981\u53c2\u8003\u4e86\u4e24\u7bc7\u6587\u7ae0\uff08<a href=\"http:\/\/codeplanet.io\/principles-good-restful-api-design\/\" target=\"_blank\" rel=\"noopener\">1<\/a>\uff0c<a href=\"https:\/\/bourgeois.me\/rest\/\" target=\"_blank\" rel=\"noopener\">2<\/a>\uff09\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1122 size-full\" src=\"https:\/\/www.xiaobo.li\/notes\/wp-content\/uploads\/2018\/06\/11.jpg\" alt=\"\" width=\"598\" height=\"318\" srcset=\"https:\/\/www.xiaobo.li\/notes\/wp-content\/uploads\/2018\/06\/11.jpg 598w, https:\/\/www.xiaobo.li\/notes\/wp-content\/uploads\/2018\/06\/11-300x160.jpg 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\" \/><\/p>\n<h2>\u4e00\u3001\u534f\u8bae<\/h2>\n<p>API\u4e0e\u7528\u6237\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u603b\u662f\u4f7f\u7528<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2014\/02\/ssl_tls.html\" target=\"_blank\" rel=\"noopener\">HTTPs\u534f\u8bae<\/a>\u3002<\/p>\n<h2>\u4e8c\u3001\u57df\u540d<\/h2>\n<p>\u5e94\u8be5\u5c3d\u91cf\u5c06API\u90e8\u7f72\u5728\u4e13\u7528\u57df\u540d\u4e4b\u4e0b\u3002<\/p>\n<pre>https:\/\/api.example.com\r\n<\/pre>\n<p>\u5982\u679c\u786e\u5b9aAPI\u5f88\u7b80\u5355\uff0c\u4e0d\u4f1a\u6709\u8fdb\u4e00\u6b65\u6269\u5c55\uff0c\u53ef\u4ee5\u8003\u8651\u653e\u5728\u4e3b\u57df\u540d\u4e0b\u3002<\/p>\n<pre>https:\/\/example.org\/api\/\r\n<\/pre>\n<h2>\u4e09\u3001\u7248\u672c\uff08Versioning\uff09<\/h2>\n<p>\u5e94\u8be5\u5c06API\u7684\u7248\u672c\u53f7\u653e\u5165URL\u3002<\/p>\n<pre>https:\/\/api.example.com\/v1\/\r\n<\/pre>\n<p>\u53e6\u4e00\u79cd\u505a\u6cd5\u662f\uff0c\u5c06\u7248\u672c\u53f7\u653e\u5728HTTP\u5934\u4fe1\u606f\u4e2d\uff0c\u4f46\u4e0d\u5982\u653e\u5165URL\u65b9\u4fbf\u548c\u76f4\u89c2\u3002<a href=\"https:\/\/developer.github.com\/v3\/media\/#request-specific-version\" target=\"_blank\" rel=\"noopener\">Github<\/a>\u91c7\u7528\u8fd9\u79cd\u505a\u6cd5\u3002<\/p>\n<h2>\u56db\u3001\u8def\u5f84\uff08Endpoint\uff09<\/h2>\n<p>\u8def\u5f84\u53c8\u79f0\"\u7ec8\u70b9\"\uff08endpoint\uff09\uff0c\u8868\u793aAPI\u7684\u5177\u4f53\u7f51\u5740\u3002<\/p>\n<p>\u5728RESTful\u67b6\u6784\u4e2d\uff0c\u6bcf\u4e2a\u7f51\u5740\u4ee3\u8868\u4e00\u79cd\u8d44\u6e90\uff08resource\uff09\uff0c\u6240\u4ee5\u7f51\u5740\u4e2d\u4e0d\u80fd\u6709\u52a8\u8bcd\uff0c\u53ea\u80fd\u6709\u540d\u8bcd\uff0c\u800c\u4e14\u6240\u7528\u7684\u540d\u8bcd\u5f80\u5f80\u4e0e\u6570\u636e\u5e93\u7684\u8868\u683c\u540d\u5bf9\u5e94\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u6570\u636e\u5e93\u4e2d\u7684\u8868\u90fd\u662f\u540c\u79cd\u8bb0\u5f55\u7684\"\u96c6\u5408\"\uff08collection\uff09\uff0c\u6240\u4ee5API\u4e2d\u7684\u540d\u8bcd\u4e5f\u5e94\u8be5\u4f7f\u7528\u590d\u6570\u3002<\/p>\n<p>\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6709\u4e00\u4e2aAPI\u63d0\u4f9b\u52a8\u7269\u56ed\uff08zoo\uff09\u7684\u4fe1\u606f\uff0c\u8fd8\u5305\u62ec\u5404\u79cd\u52a8\u7269\u548c\u96c7\u5458\u7684\u4fe1\u606f\uff0c\u5219\u5b83\u7684\u8def\u5f84\u5e94\u8be5\u8bbe\u8ba1\u6210\u4e0b\u9762\u8fd9\u6837\u3002<\/p>\n<blockquote>\n<ul>\n<li>https:\/\/api.example.com\/v1\/zoos<\/li>\n<li>https:\/\/api.example.com\/v1\/animals<\/li>\n<li>https:\/\/api.example.com\/v1\/employees<\/li>\n<\/ul>\n<\/blockquote>\n<h2>\u4e94\u3001HTTP\u52a8\u8bcd<\/h2>\n<p>\u5bf9\u4e8e\u8d44\u6e90\u7684\u5177\u4f53\u64cd\u4f5c\u7c7b\u578b\uff0c\u7531HTTP\u52a8\u8bcd\u8868\u793a\u3002<\/p>\n<p>\u5e38\u7528\u7684HTTP\u52a8\u8bcd\u6709\u4e0b\u9762\u4e94\u4e2a\uff08\u62ec\u53f7\u91cc\u662f\u5bf9\u5e94\u7684SQL\u547d\u4ee4\uff09\u3002<\/p>\n<blockquote>\n<ul>\n<li>GET\uff08SELECT\uff09\uff1a\u4ece\u670d\u52a1\u5668\u53d6\u51fa\u8d44\u6e90\uff08\u4e00\u9879\u6216\u591a\u9879\uff09\u3002<\/li>\n<li>POST\uff08CREATE\uff09\uff1a\u5728\u670d\u52a1\u5668\u65b0\u5efa\u4e00\u4e2a\u8d44\u6e90\u3002<\/li>\n<li>PUT\uff08UPDATE\uff09\uff1a\u5728\u670d\u52a1\u5668\u66f4\u65b0\u8d44\u6e90\uff08\u5ba2\u6237\u7aef\u63d0\u4f9b\u6539\u53d8\u540e\u7684\u5b8c\u6574\u8d44\u6e90\uff09\u3002<\/li>\n<li>PATCH\uff08UPDATE\uff09\uff1a\u5728\u670d\u52a1\u5668\u66f4\u65b0\u8d44\u6e90\uff08\u5ba2\u6237\u7aef\u63d0\u4f9b\u6539\u53d8\u7684\u5c5e\u6027\uff09\u3002<\/li>\n<li>DELETE\uff08DELETE\uff09\uff1a\u4ece\u670d\u52a1\u5668\u5220\u9664\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u8fd8\u6709\u4e24\u4e2a\u4e0d\u5e38\u7528\u7684HTTP\u52a8\u8bcd\u3002<\/p>\n<blockquote>\n<ul>\n<li>HEAD\uff1a\u83b7\u53d6\u8d44\u6e90\u7684\u5143\u6570\u636e\u3002<\/li>\n<li>OPTIONS\uff1a\u83b7\u53d6\u4fe1\u606f\uff0c\u5173\u4e8e\u8d44\u6e90\u7684\u54ea\u4e9b\u5c5e\u6027\u662f\u5ba2\u6237\u7aef\u53ef\u4ee5\u6539\u53d8\u7684\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u4f8b\u5b50\u3002<\/p>\n<blockquote>\n<ul>\n<li>GET \/zoos\uff1a\u5217\u51fa\u6240\u6709\u52a8\u7269\u56ed<\/li>\n<li>POST \/zoos\uff1a\u65b0\u5efa\u4e00\u4e2a\u52a8\u7269\u56ed<\/li>\n<li>GET \/zoos\/ID\uff1a\u83b7\u53d6\u67d0\u4e2a\u6307\u5b9a\u52a8\u7269\u56ed\u7684\u4fe1\u606f<\/li>\n<li>PUT \/zoos\/ID\uff1a\u66f4\u65b0\u67d0\u4e2a\u6307\u5b9a\u52a8\u7269\u56ed\u7684\u4fe1\u606f\uff08\u63d0\u4f9b\u8be5\u52a8\u7269\u56ed\u7684\u5168\u90e8\u4fe1\u606f\uff09<\/li>\n<li>PATCH \/zoos\/ID\uff1a\u66f4\u65b0\u67d0\u4e2a\u6307\u5b9a\u52a8\u7269\u56ed\u7684\u4fe1\u606f\uff08\u63d0\u4f9b\u8be5\u52a8\u7269\u56ed\u7684\u90e8\u5206\u4fe1\u606f\uff09<\/li>\n<li>DELETE \/zoos\/ID\uff1a\u5220\u9664\u67d0\u4e2a\u52a8\u7269\u56ed<\/li>\n<li>GET \/zoos\/ID\/animals\uff1a\u5217\u51fa\u67d0\u4e2a\u6307\u5b9a\u52a8\u7269\u56ed\u7684\u6240\u6709\u52a8\u7269<\/li>\n<li>DELETE \/zoos\/ID\/animals\/ID\uff1a\u5220\u9664\u67d0\u4e2a\u6307\u5b9a\u52a8\u7269\u56ed\u7684\u6307\u5b9a\u52a8\u7269<\/li>\n<\/ul>\n<\/blockquote>\n<h2>\u516d\u3001\u8fc7\u6ee4\u4fe1\u606f\uff08Filtering\uff09<\/h2>\n<p>\u5982\u679c\u8bb0\u5f55\u6570\u91cf\u5f88\u591a\uff0c\u670d\u52a1\u5668\u4e0d\u53ef\u80fd\u90fd\u5c06\u5b83\u4eec\u8fd4\u56de\u7ed9\u7528\u6237\u3002API\u5e94\u8be5\u63d0\u4f9b\u53c2\u6570\uff0c\u8fc7\u6ee4\u8fd4\u56de\u7ed3\u679c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u53c2\u6570\u3002<\/p>\n<blockquote>\n<ul>\n<li>?limit=10\uff1a\u6307\u5b9a\u8fd4\u56de\u8bb0\u5f55\u7684\u6570\u91cf<\/li>\n<li>?offset=10\uff1a\u6307\u5b9a\u8fd4\u56de\u8bb0\u5f55\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002<\/li>\n<li>?page=2&amp;per_page=100\uff1a\u6307\u5b9a\u7b2c\u51e0\u9875\uff0c\u4ee5\u53ca\u6bcf\u9875\u7684\u8bb0\u5f55\u6570\u3002<\/li>\n<li>?sortby=name&amp;order=asc\uff1a\u6307\u5b9a\u8fd4\u56de\u7ed3\u679c\u6309\u7167\u54ea\u4e2a\u5c5e\u6027\u6392\u5e8f\uff0c\u4ee5\u53ca\u6392\u5e8f\u987a\u5e8f\u3002<\/li>\n<li>?animal_type_id=1\uff1a\u6307\u5b9a\u7b5b\u9009\u6761\u4ef6<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u53c2\u6570\u7684\u8bbe\u8ba1\u5141\u8bb8\u5b58\u5728\u5197\u4f59\uff0c\u5373\u5141\u8bb8API\u8def\u5f84\u548cURL\u53c2\u6570\u5076\u5c14\u6709\u91cd\u590d\u3002\u6bd4\u5982\uff0cGET \/zoo\/ID\/animals \u4e0e GET \/animals?zoo_id=ID \u7684\u542b\u4e49\u662f\u76f8\u540c\u7684\u3002<\/p>\n<h2>\u4e03\u3001\u72b6\u6001\u7801\uff08Status Codes\uff09<\/h2>\n<p>\u670d\u52a1\u5668\u5411\u7528\u6237\u8fd4\u56de\u7684\u72b6\u6001\u7801\u548c\u63d0\u793a\u4fe1\u606f\uff0c\u5e38\u89c1\u7684\u6709\u4ee5\u4e0b\u4e00\u4e9b\uff08\u65b9\u62ec\u53f7\u4e2d\u662f\u8be5\u72b6\u6001\u7801\u5bf9\u5e94\u7684HTTP\u52a8\u8bcd\uff09\u3002<\/p>\n<blockquote>\n<ul>\n<li>200 OK - [GET]\uff1a\u670d\u52a1\u5668\u6210\u529f\u8fd4\u56de\u7528\u6237\u8bf7\u6c42\u7684\u6570\u636e\uff0c\u8be5\u64cd\u4f5c\u662f\u5e42\u7b49\u7684\uff08Idempotent\uff09\u3002<\/li>\n<li>201 CREATED - [POST\/PUT\/PATCH]\uff1a\u7528\u6237\u65b0\u5efa\u6216\u4fee\u6539\u6570\u636e\u6210\u529f\u3002<\/li>\n<li>202 Accepted - [*]\uff1a\u8868\u793a\u4e00\u4e2a\u8bf7\u6c42\u5df2\u7ecf\u8fdb\u5165\u540e\u53f0\u6392\u961f\uff08\u5f02\u6b65\u4efb\u52a1\uff09<\/li>\n<li>204 NO CONTENT - [DELETE]\uff1a\u7528\u6237\u5220\u9664\u6570\u636e\u6210\u529f\u3002<\/li>\n<li>400 INVALID REQUEST - [POST\/PUT\/PATCH]\uff1a\u7528\u6237\u53d1\u51fa\u7684\u8bf7\u6c42\u6709\u9519\u8bef\uff0c\u670d\u52a1\u5668\u6ca1\u6709\u8fdb\u884c\u65b0\u5efa\u6216\u4fee\u6539\u6570\u636e\u7684\u64cd\u4f5c\uff0c\u8be5\u64cd\u4f5c\u662f\u5e42\u7b49\u7684\u3002<\/li>\n<li>401 Unauthorized - [*]\uff1a\u8868\u793a\u7528\u6237\u6ca1\u6709\u6743\u9650\uff08\u4ee4\u724c\u3001\u7528\u6237\u540d\u3001\u5bc6\u7801\u9519\u8bef\uff09\u3002<\/li>\n<li>403 Forbidden - [*] \u8868\u793a\u7528\u6237\u5f97\u5230\u6388\u6743\uff08\u4e0e401\u9519\u8bef\u76f8\u5bf9\uff09\uff0c\u4f46\u662f\u8bbf\u95ee\u662f\u88ab\u7981\u6b62\u7684\u3002<\/li>\n<li>404 NOT FOUND - [*]\uff1a\u7528\u6237\u53d1\u51fa\u7684\u8bf7\u6c42\u9488\u5bf9\u7684\u662f\u4e0d\u5b58\u5728\u7684\u8bb0\u5f55\uff0c\u670d\u52a1\u5668\u6ca1\u6709\u8fdb\u884c\u64cd\u4f5c\uff0c\u8be5\u64cd\u4f5c\u662f\u5e42\u7b49\u7684\u3002<\/li>\n<li>406 Not Acceptable - [GET]\uff1a\u7528\u6237\u8bf7\u6c42\u7684\u683c\u5f0f\u4e0d\u53ef\u5f97\uff08\u6bd4\u5982\u7528\u6237\u8bf7\u6c42JSON\u683c\u5f0f\uff0c\u4f46\u662f\u53ea\u6709XML\u683c\u5f0f\uff09\u3002<\/li>\n<li>410 Gone -[GET]\uff1a\u7528\u6237\u8bf7\u6c42\u7684\u8d44\u6e90\u88ab\u6c38\u4e45\u5220\u9664\uff0c\u4e14\u4e0d\u4f1a\u518d\u5f97\u5230\u7684\u3002<\/li>\n<li>422 Unprocesable entity - [POST\/PUT\/PATCH] \u5f53\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u53d1\u751f\u4e00\u4e2a\u9a8c\u8bc1\u9519\u8bef\u3002<\/li>\n<li>500 INTERNAL SERVER ERROR - [*]\uff1a\u670d\u52a1\u5668\u53d1\u751f\u9519\u8bef\uff0c\u7528\u6237\u5c06\u65e0\u6cd5\u5224\u65ad\u53d1\u51fa\u7684\u8bf7\u6c42\u662f\u5426\u6210\u529f\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<p>\u72b6\u6001\u7801\u7684\u5b8c\u5168\u5217\u8868\u53c2\u89c1<a href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html\" target=\"_blank\" rel=\"noopener\">\u8fd9\u91cc<\/a>\u3002<\/p>\n<h2>\u516b\u3001\u9519\u8bef\u5904\u7406\uff08Error handling\uff09<\/h2>\n<p>\u5982\u679c\u72b6\u6001\u7801\u662f4xx\uff0c\u5c31\u5e94\u8be5\u5411\u7528\u6237\u8fd4\u56de\u51fa\u9519\u4fe1\u606f\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fd4\u56de\u7684\u4fe1\u606f\u4e2d\u5c06error\u4f5c\u4e3a\u952e\u540d\uff0c\u51fa\u9519\u4fe1\u606f\u4f5c\u4e3a\u952e\u503c\u5373\u53ef\u3002<\/p>\n<pre class=\" language-javascript\">{\r\n    error:\"Invalid API key\"\r\n}<\/pre>\n<h2>\u4e5d\u3001\u8fd4\u56de\u7ed3\u679c<\/h2>\n<p>\u9488\u5bf9\u4e0d\u540c\u64cd\u4f5c\uff0c\u670d\u52a1\u5668\u5411\u7528\u6237\u8fd4\u56de\u7684\u7ed3\u679c\u5e94\u8be5\u7b26\u5408\u4ee5\u4e0b\u89c4\u8303\u3002<\/p>\n<blockquote>\n<ul>\n<li>GET \/collection\uff1a\u8fd4\u56de\u8d44\u6e90\u5bf9\u8c61\u7684\u5217\u8868\uff08\u6570\u7ec4\uff09<\/li>\n<li>GET \/collection\/resource\uff1a\u8fd4\u56de\u5355\u4e2a\u8d44\u6e90\u5bf9\u8c61<\/li>\n<li>POST \/collection\uff1a\u8fd4\u56de\u65b0\u751f\u6210\u7684\u8d44\u6e90\u5bf9\u8c61<\/li>\n<li>PUT \/collection\/resource\uff1a\u8fd4\u56de\u5b8c\u6574\u7684\u8d44\u6e90\u5bf9\u8c61<\/li>\n<li>PATCH \/collection\/resource\uff1a\u8fd4\u56de\u5b8c\u6574\u7684\u8d44\u6e90\u5bf9\u8c61<\/li>\n<li>DELETE \/collection\/resource\uff1a\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6587\u6863<\/li>\n<\/ul>\n<\/blockquote>\n<h2>\u5341\u3001Hypermedia API<\/h2>\n<p>RESTful API\u6700\u597d\u505a\u5230Hypermedia\uff0c\u5373\u8fd4\u56de\u7ed3\u679c\u4e2d\u63d0\u4f9b\u94fe\u63a5\uff0c\u8fde\u5411\u5176\u4ed6API\u65b9\u6cd5\uff0c\u4f7f\u5f97\u7528\u6237\u4e0d\u67e5\u6587\u6863\uff0c\u4e5f\u77e5\u9053\u4e0b\u4e00\u6b65\u5e94\u8be5\u505a\u4ec0\u4e48\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u5f53\u7528\u6237\u5411api.example.com\u7684\u6839\u76ee\u5f55\u53d1\u51fa\u8bf7\u6c42\uff0c\u4f1a\u5f97\u5230\u8fd9\u6837\u4e00\u4e2a\u6587\u6863\u3002<\/p>\n<pre class=\" language-javascript\">{\r\n \"link\": {\r\n  \"rel\":   \"collection <a href=\"https:\/\/www.example.com\/zoos\">https:\/\/www.example.com\/zoos<\/a>\",\r\n  \"href\":  \"<a href=\"https:\/\/api.example.com\/zoos\">https:\/\/api.example.com\/zoos<\/a>\",\r\n  \"title\": \"List of zoos\",\r\n  \"type\":\"application\/vnd.yourformat+json\"\r\n<span class=\"token punctuation\"> }\r\n<\/span><span class=\"token punctuation\">}<\/span><\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u8868\u793a\uff0c\u6587\u6863\u4e2d\u6709\u4e00\u4e2alink\u5c5e\u6027\uff0c\u7528\u6237\u8bfb\u53d6\u8fd9\u4e2a\u5c5e\u6027\u5c31\u77e5\u9053\u4e0b\u4e00\u6b65\u8be5\u8c03\u7528\u4ec0\u4e48API\u4e86\u3002rel\u8868\u793a\u8fd9\u4e2aAPI\u4e0e\u5f53\u524d\u7f51\u5740\u7684\u5173\u7cfb\uff08collection\u5173\u7cfb\uff0c\u5e76\u7ed9\u51fa\u8be5collection\u7684\u7f51\u5740\uff09\uff0chref\u8868\u793aAPI\u7684\u8def\u5f84\uff0ctitle\u8868\u793aAPI\u7684\u6807\u9898\uff0ctype\u8868\u793a\u8fd4\u56de\u7c7b\u578b\u3002<\/p>\n<p>Hypermedia API\u7684\u8bbe\u8ba1\u88ab\u79f0\u4e3a<a href=\"http:\/\/en.wikipedia.org\/wiki\/HATEOAS\" target=\"_blank\" rel=\"noopener\">HATEOAS<\/a>\u3002Github\u7684API\u5c31\u662f\u8fd9\u79cd\u8bbe\u8ba1\uff0c\u8bbf\u95ee<a href=\"https:\/\/api.github.com\/\" target=\"_blank\" rel=\"noopener\">api.github.com<\/a>\u4f1a\u5f97\u5230\u4e00\u4e2a\u6240\u6709\u53ef\u7528API\u7684\u7f51\u5740\u5217\u8868\u3002<\/p>\n<pre>{\r\n  \"current_user_url\": \"<a href=\"https:\/\/api.github.com\/user\">https:\/\/api.github.com\/user<\/a>\",\r\n  \"authorizations_url\": \"<a href=\"https:\/\/api.github.com\/authorizations\">https:\/\/api.github.com\/authorizations<\/a>\",\r\n <span spellcheck=\"true\"> \/\/ ...\r\n<\/span>}\r\n<\/pre>\n<p>\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u5230\uff0c\u5982\u679c\u60f3\u83b7\u53d6\u5f53\u524d\u7528\u6237\u7684\u4fe1\u606f\uff0c\u5e94\u8be5\u53bb\u8bbf\u95ee<a href=\"https:\/\/api.github.com\/user\" target=\"_blank\" rel=\"noopener\">api.github.com\/user<\/a>\uff0c\u7136\u540e\u5c31\u5f97\u5230\u4e86\u4e0b\u9762\u7ed3\u679c\u3002<\/p>\n<pre>{\r\n  \"message\": \"Requires authentication\",\r\n  \"documentation_url\": \"<a href=\"https:\/\/developer.github.com\/v3\">https:\/\/developer.github.com\/v3<\/a>\"\r\n}\r\n<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u8868\u793a\uff0c\u670d\u52a1\u5668\u7ed9\u51fa\u4e86\u63d0\u793a\u4fe1\u606f\uff0c\u4ee5\u53ca\u6587\u6863\u7684\u7f51\u5740\u3002<\/p>\n<h2>\u5341\u4e00\u3001\u5176\u4ed6<\/h2>\n<p>\uff081\uff09API\u7684\u8eab\u4efd\u8ba4\u8bc1\u5e94\u8be5\u4f7f\u7528<a href=\"http:\/\/www.ruanyifeng.com\/blog\/2014\/05\/oauth_2_0.html\" target=\"_blank\" rel=\"noopener\">OAuth 2.0<\/a>\u6846\u67b6\u3002<\/p>\n<p>\uff082\uff09\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\u683c\u5f0f\uff0c\u5e94\u8be5\u5c3d\u91cf\u4f7f\u7528JSON\uff0c\u907f\u514d\u4f7f\u7528XML\u3002<\/p>\n<p>\uff08\u5b8c\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u6587\u7ae0\u6458\u81ea\uff1a http:\/\/www.ruanyifeng.com\/blog\/2014\/05\/restful_api.html<\/p>\n<p>\u53c2\u8003\uff1a<\/p>\n<p>\u7406\u89e3RESTful\u67b6\u6784 <a href=\"http:\/\/www.ruanyifeng.com\/blog\/2011\/09\/restful.html\" target=\"_blank\" rel=\"noopener\">http:\/\/www.ruanyifeng.com\/blog\/2011\/09\/restful.html<\/a><\/p>\n<p>\u597dRESTful API\u7684\u8bbe\u8ba1\u539f\u5219\u00a0 <a href=\"https:\/\/www.cnblogs.com\/moonz-wu\/p\/4211626.html\" target=\"_blank\" rel=\"noopener\">https:\/\/www.cnblogs.com\/moonz-wu\/p\/4211626.html<\/a><\/p>\n<p><a href=\"http:\/\/www.informit.com\/articles\/article.aspx?p=1566460\" target=\"_blank\" rel=\"noopener\">http:\/\/www.informit.com\/articles\/article.aspx?p=1566460<\/a><\/p>\n<p>..<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\uff0c\u5206\u4e3a\u524d\u7aef\u548c\u540e\u7aef\u4e24\u4e2a\u90e8\u5206\u3002\u5f53\u524d\u7684\u53d1\u5c55\u8d8b\u52bf\uff0c\u5c31\u662f\u524d\u7aef\u8bbe\u5907\u5c42\u51fa\u4e0d\u7a77\uff08\u624b\u673a &hellip; <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/1121\">\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":[284],"tags":[],"class_list":["post-1121","post","type-post","status-publish","format-standard","hentry","category-webserver"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}