{"id":6503,"date":"2024-08-23T16:47:11","date_gmt":"2024-08-23T08:47:11","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6503"},"modified":"2024-08-23T16:47:11","modified_gmt":"2024-08-23T08:47:11","slug":"restful-api-%e8%ae%be%e8%ae%a1%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6503","title":{"rendered":"Restful API \u8bbe\u8ba1\u6307\u5357"},"content":{"rendered":"<blockquote>\n<p>\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210<\/p>\n<\/blockquote>\n<p>\u8bbe\u8ba1 RESTful API \u7684\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd API \u7684\u4e00\u81f4\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8bbe\u8ba1 RESTful API \u7684\u6307\u5357\uff1a<\/p>\n<h3>1. <strong>\u8d44\u6e90\u8bbe\u8ba1<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u8d44\u6e90\u5b9a\u4e49<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8d44\u6e90\u662f API \u7684\u6838\u5fc3\uff0c\u901a\u5e38\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u9700\u8981\u7ba1\u7406\u7684\u5bf9\u8c61\u6216\u6570\u636e\u3002\u786e\u4fdd\u6bcf\u4e2a\u8d44\u6e90\u6709\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff08\u901a\u5e38\u662f URI\uff09\u3002<\/li>\n<li>\u4f8b\u5982\uff0c<code>\/users<\/code> \u4ee3\u8868\u7528\u6237\u8d44\u6e90\uff0c<code>\/products<\/code> \u4ee3\u8868\u4ea7\u54c1\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8d44\u6e90\u7684\u5c42\u6b21\u7ed3\u6784<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u5c42\u6b21\u7ed3\u6784\u6765\u8868\u793a\u8d44\u6e90\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u4f8b\u5982\uff1a<\/li>\n<li><code>\/users\/{userId}\/orders<\/code>\uff1a\u8868\u793a\u7279\u5b9a\u7528\u6237\u7684\u8ba2\u5355\u3002<\/li>\n<li><code>\/products\/{productId}\/reviews<\/code>\uff1a\u8868\u793a\u7279\u5b9a\u4ea7\u54c1\u7684\u8bc4\u8bba\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>2. <strong>HTTP \u65b9\u6cd5\u7684\u4f7f\u7528<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>GET<\/strong>\uff1a\u7528\u4e8e\u68c0\u7d22\u8d44\u6e90\u3002<\/p>\n<ul>\n<li>\u793a\u4f8b\uff1a<code>GET \/users<\/code>\uff08\u83b7\u53d6\u7528\u6237\u5217\u8868\uff09\uff0c<code>GET \/users\/{userId}<\/code>\uff08\u83b7\u53d6\u7279\u5b9a\u7528\u6237\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>POST<\/strong>\uff1a\u7528\u4e8e\u521b\u5efa\u8d44\u6e90\u3002<\/p>\n<ul>\n<li>\u793a\u4f8b\uff1a<code>POST \/users<\/code>\uff08\u521b\u5efa\u65b0\u7528\u6237\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>PUT<\/strong>\uff1a\u7528\u4e8e\u66f4\u65b0\u73b0\u6709\u8d44\u6e90\u3002\u901a\u5e38\u7528\u4e8e\u66ff\u6362\u6574\u4e2a\u8d44\u6e90\u3002<\/p>\n<ul>\n<li>\u793a\u4f8b\uff1a<code>PUT \/users\/{userId}<\/code>\uff08\u66f4\u65b0\u7279\u5b9a\u7528\u6237\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>PATCH<\/strong>\uff1a\u7528\u4e8e\u90e8\u5206\u66f4\u65b0\u8d44\u6e90\u3002<\/p>\n<ul>\n<li>\u793a\u4f8b\uff1a<code>PATCH \/users\/{userId}<\/code>\uff08\u66f4\u65b0\u7279\u5b9a\u7528\u6237\u7684\u90e8\u5206\u5b57\u6bb5\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>DELETE<\/strong>\uff1a\u7528\u4e8e\u5220\u9664\u8d44\u6e90\u3002<\/p>\n<ul>\n<li>\u793a\u4f8b\uff1a<code>DELETE \/users\/{userId}<\/code>\uff08\u5220\u9664\u7279\u5b9a\u7528\u6237\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3. <strong>URI \u8bbe\u8ba1<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u7b80\u6d01\u660e\u4e86<\/strong>\uff1a<\/p>\n<ul>\n<li>URI \u5e94\u8be5\u7b80\u6d01\u3001\u6e05\u6670\u4e14\u5177\u63cf\u8ff0\u6027\u3002\u4f8b\u5982\uff1a<code>\/products<\/code>\uff0c\u800c\u4e0d\u662f <code>\/getProducts<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8d44\u6e90\u6807\u8bc6<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u540d\u8bcd\u6765\u8868\u793a\u8d44\u6e90\u800c\u4e0d\u662f\u52a8\u8bcd\u3002\u4f8b\u5982\uff0c<code>\/orders<\/code> \u4ee3\u66ff <code>\/createOrder<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4e00\u81f4\u6027<\/strong>\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd URI \u7ed3\u6784\u7684\u4e00\u81f4\u6027\u3002\u4f8b\u5982\uff1a<code>\/users\/{userId}\/posts<\/code> \u8868\u793a\u7528\u6237\u7684\u5e16\u5b50\uff0c\u800c <code>users\/{userId}\/comments<\/code> \u8868\u793a\u7528\u6237\u7684\u8bc4\u8bba\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>4. <strong>\u8bf7\u6c42\u548c\u54cd\u5e94\u683c\u5f0f<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>JSON \u683c\u5f0f<\/strong>\uff1a<\/p>\n<ul>\n<li>\u63a8\u8350\u4f7f\u7528 JSON \u4f5c\u4e3a\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u56e0\u5176\u7b80\u5355\u6613\u8bfb\u4e14\u5e7f\u6cdb\u652f\u6301\u3002<\/li>\n<li>\u793a\u4f8b\u8bf7\u6c42\uff1a\n<pre><code class=\"language-json\">{\n\"name\": \"John Doe\",\n\"email\": \"john@example.com\"\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u54cd\u5e94\u72b6\u6001\u7801<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>200 OK<\/strong>\uff1a\u8bf7\u6c42\u6210\u529f\u3002<\/li>\n<li><strong>201 Created<\/strong>\uff1a\u6210\u529f\u521b\u5efa\u8d44\u6e90\u3002<\/li>\n<li><strong>204 No Content<\/strong>\uff1a\u6210\u529f\u5904\u7406\u8bf7\u6c42\u4f46\u65e0\u5185\u5bb9\u8fd4\u56de\u3002<\/li>\n<li><strong>400 Bad Request<\/strong>\uff1a\u8bf7\u6c42\u683c\u5f0f\u9519\u8bef\u3002<\/li>\n<li><strong>401 Unauthorized<\/strong>\uff1a\u672a\u6388\u6743\u8bbf\u95ee\u3002<\/li>\n<li><strong>404 Not Found<\/strong>\uff1a\u8d44\u6e90\u672a\u627e\u5230\u3002<\/li>\n<li><strong>500 Internal Server Error<\/strong>\uff1a\u670d\u52a1\u5668\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a<\/p>\n<ul>\n<li>\u8fd4\u56de\u4e00\u81f4\u7684\u9519\u8bef\u7ed3\u6784\uff0c\u5305\u542b\u9519\u8bef\u4ee3\u7801\u548c\u63cf\u8ff0\u4fe1\u606f\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-json\">{\n\"error\": \"Invalid user ID\",\n\"message\": \"The user ID provided is not valid.\"\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>5. <strong>\u5206\u9875\u548c\u8fc7\u6ee4<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u5206\u9875<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u8fd4\u56de\u5927\u91cf\u6570\u636e\u7684\u8bf7\u6c42\uff0c\u4f7f\u7528\u5206\u9875\u6765\u9650\u5236\u6bcf\u6b21\u8fd4\u56de\u7684\u6570\u636e\u91cf\u3002\u4f8b\u5982\uff1a<\/li>\n<li><code>GET \/products?page=1&amp;limit=20<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8fc7\u6ee4<\/strong>\uff1a<\/p>\n<ul>\n<li>\u63d0\u4f9b\u8fc7\u6ee4\u53c2\u6570\u4ee5\u5141\u8bb8\u5ba2\u6237\u7aef\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u7b5b\u9009\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/li>\n<li><code>GET \/products?category=electronics&amp;price_max=1000<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>6. <strong>\u7248\u672c\u63a7\u5236<\/strong><\/h3>\n<ul>\n<li><strong>\u7248\u672c\u5316 API<\/strong>\uff1a\n<ul>\n<li>\u5728 URL \u4e2d\u5305\u542b\u7248\u672c\u53f7\uff0c\u786e\u4fdd API \u7684\u5411\u540e\u517c\u5bb9\u6027\u3002\u4f8b\u5982\uff1a<\/li>\n<li><code>\/v1\/users<\/code><\/li>\n<li><code>\/v2\/users<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>7. <strong>\u5b89\u5168\u6027<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u8ba4\u8bc1<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u8ba4\u8bc1\u673a\u5236\u6765\u4fdd\u62a4 API\uff0c\u4f8b\u5982 OAuth\u3001JWT\uff08JSON Web Token\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6388\u6743<\/strong>\uff1a<\/p>\n<ul>\n<li>\u786e\u4fdd\u7528\u6237\u6709\u6743\u8bbf\u95ee\u6216\u64cd\u4f5c\u8bf7\u6c42\u7684\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8f93\u5165\u9a8c\u8bc1<\/strong>\uff1a<\/p>\n<ul>\n<li>\u9a8c\u8bc1\u6240\u6709\u4f20\u5165\u7684\u6570\u636e\uff0c\u9632\u6b62\u6076\u610f\u8f93\u5165\u548c\u653b\u51fb\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>8. <strong>\u6587\u6863\u548c\u6d4b\u8bd5<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u6587\u6863<\/strong>\uff1a<\/p>\n<ul>\n<li>\u63d0\u4f9b\u6e05\u6670\u7684 API \u6587\u6863\uff0c\u63cf\u8ff0\u5404\u4e2a\u7aef\u70b9\u3001\u8bf7\u6c42\u548c\u54cd\u5e94\u683c\u5f0f\u3001\u72b6\u6001\u7801\u548c\u793a\u4f8b\u3002<\/li>\n<li>\u5de5\u5177\u5982 Swagger\/OpenAPI \u53ef\u4ee5\u5e2e\u52a9\u751f\u6210\u548c\u7ef4\u62a4\u6587\u6863\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6d4b\u8bd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\u6d4b\u8bd5 API \u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u5355\u5143\u6d4b\u8bd5\u3001\u96c6\u6210\u6d4b\u8bd5\u548c\u7aef\u5230\u7aef\u6d4b\u8bd5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>9. <strong>\u7f13\u5b58<\/strong><\/h3>\n<ul>\n<li><strong>\u7f13\u5b58\u7b56\u7565<\/strong>\uff1a\n<ul>\n<li>\u4f7f\u7528\u7f13\u5b58\u673a\u5236\u63d0\u9ad8\u6027\u80fd\uff0c\u4f8b\u5982\u901a\u8fc7\u8bbe\u7f6e <code>Cache-Control<\/code> \u5934\u6765\u63a7\u5236\u7f13\u5b58\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>10. <strong>\u8d85\u5a92\u4f53\u4f5c\u4e3a\u5e94\u7528\u72b6\u6001\u5f15\u64ce (HATEOAS)<\/strong><\/h3>\n<ul>\n<li><strong>HATEOAS<\/strong>\uff1a\n<ul>\n<li>\u901a\u8fc7\u5728\u54cd\u5e94\u4e2d\u5305\u542b\u94fe\u63a5\uff0c\u5e2e\u52a9\u5ba2\u6237\u7aef\u53d1\u73b0\u548c\u4f7f\u7528 API \u63d0\u4f9b\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-json\">{\n\"userId\": 1,\n\"name\": \"John Doe\",\n\"links\": {\n\"self\": \"\/users\/1\",\n\"orders\": \"\/users\/1\/orders\"\n}\n}<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u8bbe\u8ba1 RESTful API \u65f6\uff0c\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd API \u7684\u53ef\u7528\u6027\u3001\u4e00\u81f4\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u660e\u786e\u7684\u8d44\u6e90\u8bbe\u8ba1\u3001\u5408\u7406\u7684 HTTP \u65b9\u6cd5\u4f7f\u7528\u3001\u6e05\u6670\u7684 URI \u8bbe\u8ba1\u3001\u826f\u597d\u7684\u8bf7\u6c42\u54cd\u5e94\u683c\u5f0f\u548c\u5408\u9002\u7684\u5b89\u5168\u63aa\u65bd\u90fd\u662f\u6210\u529f API \u8bbe\u8ba1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u975e\u539f\u521b\uff0cChatGPT \u95ee\u7b54\u751f\u6210 \u8bbe\u8ba1 RESTful API \u7684\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u786e\u4fdd API \u7684\u4e00\u81f4\u6027\u3001\u53ef\u7ef4&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6503\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-6503","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6503"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6503\/revisions"}],"predecessor-version":[{"id":6504,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6503\/revisions\/6504"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}