{"id":455,"date":"2018-10-16T22:00:10","date_gmt":"2018-10-16T14:00:10","guid":{"rendered":"http:\/\/develop-note.com\/blog\/?p=455"},"modified":"2022-02-16T22:16:32","modified_gmt":"2022-02-16T14:16:32","slug":"2019ironman-flask-restful-day15","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2018\/10\/16\/2019ironman-flask-restful-day15\/","title":{"rendered":"\u7b2c\u5341\u5c46\u9435\u4eba\u8cfd flask-restful DAY15-\u641e\u61c2\u5982\u4f55\u5efa\u7acbResource"},"content":{"rendered":"<h1>flask-restful\u8d77\u624b\u5f0f<\/h1>\n<p>\u5728\u7d93\u904eflask\u57fa\u672c\u6559\u7a0b\u3001restful\u4ee5\u53caPOSTMAN\u7684\u6559\u7a0b\u5f8c\u7d42\u65bc\u8981\u6e96\u5099\u9032\u5165flask-restful\u7684\u6559\u7a0b\u4e86\uff0c\u4eca\u65e5\u7684\u6559\u7a0b\u6559\u6388\u5927\u5bb6\u5982\u4f55\u4f7f\u7528flask-restful\u3001\u5982\u4f55\u914d\u7f6e\u7a0b\u5f0f\u78bc\u4ee5\u53ca\u900f\u904ePOSTMAN\u767c\u51fa\u7b2c\u4e00\u500b\u8acb\u6c42\u3002<!--more--><\/p>\n<h1>\u5b89\u88ddflask-restful<\/h1>\n<p>\u5728\u4f7f\u7528\u4e4b\u524d\u4e0d\u5916\u4e4e\u5c31\u662f\u5b89\u88dd\u51fd\u5f0f\u5eab\u4e86\uff0c\u76f8\u4fe1\u7d93\u904e\u4e4b\u524d\u6559\u7a0b\u5404\u4f4d\u8b80\u8005\u61c9\u8a72\u6709\u60f3\u6cd5\u5982\u4f55\u5b89\u88dd\uff0c\u82e5\u662f\u6c92\u6709\u60f3\u6cd5\u8acb\u770b\u4ee5\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-bash\">$ pip install flask-restful<\/code><\/pre>\n<p>\u76f8\u4fe1\u8f38\u5165\u4e0a\u8ff0\u6307\u4ee4\u7684\u5927\u5bb6\u61c9\u8a72\u53ef\u4ee5\u9806\u5229\u5b89\u88dd\u5b8c\u6210flask-restful\u3002<\/p>\n<h1>\u7b2c\u4e00\u500bresource<\/h1>\n<p>\u5728\u5b8c\u6210flask-restful\u5b89\u88dd\u5f8c\u53ef\u4ee5\u958b\u59cbrestful\u7684\u958b\u767c\u4e86\uff0c\u4eca\u5929\u5148\u7c21\u55ae\u4ecb\u7d39\u5927\u5bb6\u5982\u4f55\u5beb\u4e00\u500brestful api\uff0c\u6240\u4ee5\u7c21\u55ae\u6559\u5b78\u5148\u628a\u6240\u6709\u7684\u5167\u5bb9\u5beb\u5728\u4e00\u500b<code><code>app.py<\/code><\/code>\u4e0a\u9762\uff0c\u4e0d\u904e\u9019\u4e0d\u662f\u4e00\u500b\u597d\u7684\u793a\u7bc4\uff0c\u56e0\u70ba\u6240\u6709\u7684\u5167\u5bb9\u90fd\u5beb\u5728\u4e00\u500b\u6a94\u6848\u8981\u600e\u9ebc\u7dad\u8b77\u963f\u3002\u6240\u4ee5\u5f8c\u7e8c\u6703\u4ecb\u7d39flask-restful\u8981\u5982\u4f55\u914d\u7f6e\uff0c\u4f46\u5404\u4f4d\u8b80\u8005\u8acb\u5148\u770b\u770b\u4ee5\u4e0b\u4f8b\u5b50\uff1a<\/p>\n<h2>\u8d77\u624b\u5f0f<\/h2>\n<pre><code class=\"language-python\">from flask import Flask\n\napp = Flask(__name__)\n\nif __name__ == &quot;__main__&quot;:\n    app.run()<\/code><\/pre>\n<p>\u76f8\u4fe1\u5927\u5bb6\u5c0d\u9019\u4f8b\u5b50\u4e0d\u964c\u751f\u5427\uff0c\u9019\u5c31\u662f\u6700\u4e00\u958b\u59cb\u4ecb\u7d39\u7d66\u5927\u5bb6\u7684\u4f8b\u5b50\uff0c\u7576\u7136flask-restful\u4e5f\u8981\u5f9e\u9019\u908a\u51fa\u767c\uff0c\u6240\u4ee5\u6211\u5011\u6dfb\u52a0\u4e00\u4e9b\u6771\u897f\u5f8c\uff0c\u5c31\u6210\u4e86\u4e0b\u5217\u4f8b\u5b50\u4e86\uff1a<\/p>\n<pre><code class=\"language-python\">from flask import Flask\nfrom flask_restful import Api\n\napp = Flask(__name__)\napi = Api(app)\n\nif __name__ == &quot;__main__&quot;:\n    app.run()<\/code><\/pre>\n<p>\u5c31\u9019\u6a23flask-restful\u7684\u521d\u59cb\u5316\u5c31\u5b8c\u6210\u4e86\uff0c\u7136\u800c\u5118\u7ba1\u5982\u6b64\u9084\u6c92\u6709\u8fa6\u6cd5\u63d0\u4f9bapi\u7d66\u4f7f\u7528\u8005\u63d0\u4ea4\u8acb\u6c42\uff0c\u6240\u4ee5\u63a5\u4e0b\u4f86\u8981\u505a\u7684\u4e8b\u5c31\u662f\u65b0\u589e\u4e00\u500bresourece\uff0c\u800crestful\u5c31\u662f\u4ee5\u8cc7\u6e90\u70ba\u57fa\u790e\uff0c\u6bcf\u4e00\u500b\u9805\u76ee\u90fd\u53ef\u4ee5\u662f\u4e00\u500b\u8cc7\u6e90\uff0c\u6240\u4ee5\u6211\u5011\u5728\u6dfb\u52a0\u4e00\u500bresource\u5728\u539f\u4f86\u7684\u5167\u5bb9\u4e4b\u4e2d\u3002<\/p>\n<pre><code class=\"language-python\">from flask import Flask\nfrom flask_restful import Api\n\nfrom flask_restful import Resource\n\nclass PrintHelloWorld(Resource):\n    def get(self):\n        return {\n            &#039;message&#039;: &#039;Hello Wrold!&#039;\n        }, 200\n\napp = Flask(__name__)\napi = Api(app)\n\napi.add_resource(PrintHelloWorld, &quot;\/print_hello_world\/&quot;)\n\nif __name__ == &quot;__main__&quot;:\n    app.run()<\/code><\/pre>\n<p>\u5230\u9019\u88e1\u5c31\u6709\u6771\u897f\u53ef\u4ee5\u8aaa\u660e\u4e86\uff0c\u9019\u88e1\u6211\u5011\u5148import\u4e00\u500b<code><code>Resource<\/code>`<code>\uff0c\u518d\u4f86\u6bcf\u500b\u9805\u76ee\u90fd\u6e90\u81ea\u65bc<\/code>`<code>Resource<\/code>`<code>\uff0c\u6240\u4ee5\u6211\u5011\u5efa\u7acb\u4e00\u500b\u65b0\u7684\u9805\u76ee\u7e7c\u627f<\/code>`<code>Resource<\/code>`<code>\u5982\u540c\u4e0a\u8ff0\u4f8b\u5b50PrintHelloWorld\uff0c\u518d\u4f86\u6211\u5011\u6642\u505a\u4e00\u500b\u7c21\u55ae\u7684\u65b9\u6cd5<\/code>`<code>get<\/code>`<code>\u9019\u4e5f\u547c\u61c9\u5230\u5230\u6642\u5ba2\u6236\u7aef\u7684\u8acb\u6c42\u6240\u9700\u63d0\u4ea4\u7684<\/code>`<code>method<\/code>`<code>\uff0c\u4e4b\u5f8c\u5728<\/code>`<code>get<\/code>`<code>\u5167\u5be6\u4f5c\u7576\u4f3a\u670d\u5668\u7aef\u6536\u5230\u9019<\/code>`<code>method<\/code>`<code>\u7684\u8acb\u6c42\u8981\u505a\u751a\u9ebc\uff0c\u4ee5\u53ca\u8fd4\u9084\u751a\u9ebc\u8cc7\u8a0a\u7d66\u5ba2\u6236\u7aef\u3002\u5728\u6211\u5011\u9019\u4f8b\u5b50\u5c31\u7c21\u55ae\u8fd4\u9084\u4e00\u500bJSON\u7684\u8a0a\u606f\u683c\u5f0f\u4ee5\u53ca<\/code>`<code>status<\/code>`<code>\uff0c\u6700\u5f8c\u4e00\u500b\u52d5\u4f5c\u5c31\u662f\u628a\u65b0\u5efa\u7acb\u7684<\/code>`<code>Resource<\/code>`<code>\u8a3b\u518a\u5230flask-restful\u4e4b\u4e2d\uff0c\u5c31\u662f\u4f8b\u5b50\u5167\u7684<\/code>`<code>api.add_resource(PrintHelloWorld, &quot;\/print_hello_world\/&quot;)<\/code>`<code>\u9019\u5f0f\u5b50\uff0c\u5176\u4e2d\u7b2c\u4e8c\u500b\u53c3\u6578\u662f\u5c07\u9019<\/code>`<code>Resource<\/code>`<code>\u7d81\u5b9a\u5230url<\/code>`<code>\/print_hello_world\/<\/code>`<code>\uff0c\u7b2c\u4e09\u500b\u53c3\u6578\u660e\u5929\u6703\u52a0\u4ee5\u8aaa\u660e\uff0c\u5c31\u9019\u6a23\u6211\u5011\u7b2c\u4e00\u500bHello World restful api\u5c31\u5b8c\u6210\u4e86\uff0c\u63a5\u4e0b\u4f86\u6211\u5011\u770b\u770b\u8981\u600e\u9ebc\u63d0\u4ea4\u8acb\u6c42\u4f86\u547c\u53eb\u9019api\uff0c\u4e0d\u904e\u5728\u63d0\u4ea4\u8acb\u6c42\u4e4b\u524d\u8acb\u5148\u7528\u4ee5\u4e0b\u6307\u4ee4\u57f7\u884c\u9019<\/code>`<code>app.py<\/code><\/code>\u3002<\/p>\n<pre><code class=\"language-bash\">$ python app.py<\/code><\/pre>\n<p>\u5982\u679c\u6c92\u8fa6\u6cd5\u57f7\u884c\u8acb\u5148\u78ba\u5b9a\u4e00\u4e0bvirtualenv\u662f\u5426\u7528\u4e86\uff0c\u6216\u8005pip\u662f\u5426\u5b89\u88dd\u9700\u5b89\u88dd\u7684\u5957\u4ef6\u3002<\/p>\n<h1>\u7b2c\u4e00\u500bPOSTMAN\u8acb\u6c42<\/h1>\n<p><a href=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/first_request_hello_world.png\"><img decoding=\"async\" src=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/first_request_hello_world.png\" alt=\"POSTMAN \u7b2c\u4e00\u500b\u8acb\u6c42\" \/><\/a><\/p>\n<p>\u9996\u5148\u5148\u4f86\u770b\u770b\u6211\u5011\u8acb\u6c42\u7684url\uff1a<\/p>\n<p><a href=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/first_url.png\"><img decoding=\"async\" src=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/first_url.png\" alt=\"\" \/><\/a><\/p>\n<p>\u773c\u5c16\u7684\u8b80\u8005\u6703\u89ba\u5f97\u5f88\u5947\u602a\uff0c\u600e\u9ebc\u6703\u6709<code><code class=\"kb-btn\">{url}<\/code><\/code>\u9019\u7a2e\u7db2\u5740\u5462\uff0c\u9019\u5c31\u662f\u8981\u4ecb\u7d39POSTMAN\u5f37\u5927\u529f\u80fd\u4e4b\u4e00\u7684\u74b0\u5883\u8b8a\u6578\uff0c\u7576\u8b80\u8005\u5011\u9ede\u64ca\u773c\u775b\u7684\u6309\u9215\u6703\u51fa\u73fe\u4e0b\u9762\u5167\u5bb9\uff1a<\/p>\n<p><a href=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/env.png\"><img decoding=\"async\" src=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/env.png\" alt=\"POSTMAN \u74b0\u5883\u8a2d\u5b9a\u8996\u7a97\" \/><\/a><\/p>\n<p>\u5c31\u662f\u5728\u9019\u908a\u6211\u5011\u5b9a\u7fa9\u4e86<code><code class=\"kb-btn\">{url}<\/code>`<code>\u662f\u751a\u9ebc\uff0c\u6240\u4ee5\u7576\u6211\u5011\u60f3\u8981\u8acb\u6c42\u5230production\u7684\u4f3a\u670d\u5668\u6642\u5247\u4e0d\u9700\u8981\u6539\u6240\u6709\u7684\u8acb\u6c42\u96c6\u5408\uff0c\u53ea\u8981\u4fee\u6539\u74b0\u5883\u8b8a\u6578\u7684\u5167\u5bb9\u5373\u53ef\uff0c\u9019\u88e1\u773c\u5c16\u7684\u8b80\u8005\u4e5f\u770b\u5230<\/code>`<code>INITIAL VALUE<\/code>`<code>\u8ddf<\/code>`<code>CURRENT VALUE<\/code>`<code>\uff0c\u81f3\u65bc\u9019\u5169\u500b\u5167\u5bb9\u6709\u4f55\u5dee\u7570\uff0c\u5728\u6b64\u7b46\u8005\u5148\u8ce3\u500b\u95dc\u5b50\uff0c\u4e4b\u5f8c\u6703\u518d\u63a5\u8457\u4ecb\u7d39POSTMAN\u5176\u4ed6\u5f37\u5927\u529f\u80fd\u662f\u5728\u52a0\u4ee5\u8aaa\u660e\u3002\u6700\u5f8c\u7576\u6309\u4e0b<\/code>`<code>SEND<\/code><\/code>\u8b80\u8005\u5011\u61c9\u8a72\u6703\u770b\u5230\u4ee5\u4e0b\u5167\u5bb9\u3002<\/p>\n<p><a href=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/first_response.png\"><img decoding=\"async\" src=\"https:\/\/www.develop-note.com\/blog\/wp-content\/uploads\/2018\/10\/first_response.png\" alt=\"POSTMAN \u7b2c\u4e00\u500b\u56de\u61c9\" \/><\/a><\/p>\n<p>\u9019\u5c31\u662fPOSTMAN\u63a5\u6536\u5230\u4f3a\u670d\u5668\u8fd4\u9084\u7684status 200\u4ee5\u53caJSON\u683c\u5f0f\u7684response\u3002\u5230\u6b64\u5c31\u5b8c\u6210\u6211\u5011\u7b2c\u4e00\u500brestful api\u4e86\u3002<\/p>\n<h1>\u914d\u7f6e\u61c9\u7528\u7a0b\u5f0f\u7d50\u69cb<\/h1>\n<p>\u5118\u7ba1\u8b80\u8005\u60f3\u8981\u628a\u6240\u6709\u7684\u5167\u5bb9\u90fd\u5beb\u5728<code><code>app.py<\/code><\/code>\u4e5f\u662f\u53ef\u4ee5\uff0c\u4f46\u662f\u9019\u6a23\u6703\u5f88\u96e3\u7dad\u8b77\uff0c\u6240\u4ee5\u9019\u90e8\u5206\u544a\u8a34\u5927\u5bb6\u5982\u4f55\u6574\u7406flask-restful\u6a94\u6848\u7d50\u69cb\uff0c\u9019\u88e1\u8acb\u5927\u5bb6\u770b\u770b\u4e0b\u5217\u5167\u5bb9\uff1a<\/p>\n<pre><code class=\"language-bash\">myapi\/\n    __init__.py\n    app.py          # this file contains your app and routes\n    resources\/\n        __init__.py\n        foo.py      # contains logic for \/Foo\n        bar.py      # contains logic for \/Bar\n    common\/\n        __init__.py\n        util.py     # just some common infrastructure<\/code><\/pre>\n<p>\u4ee5\u4e0a\u662f\u5b98\u7db2\u63d0\u4f9b\u7684\u6a94\u6848\u7d50\u69cb\u5167\u5bb9\uff0c\u6240\u4ee5\u628a\u6240\u6709\u8cc7\u6599\u5beb\u5728\u4e00\u500bapp.py\u5c31\u5230\u4eca\u5929\u70ba\u6b62\uff0c\u660e\u5929\u958b\u59cb\u6703\u91dd\u5c0d\u6bcf\u500b<code><code>Resource<\/code><\/code>\u653e\u5728resources\u8cc7\u6599\u593e\u4e4b\u5167\u3002\u9084\u6709\u5f80\u5f8c\u5e7e\u5929\u6703\u5728\u65b0\u589e\u5e7e\u500b\u8cc7\u6599\u593e\uff0c\u5230\u6642\u5019\u518d\u8aaa\u660e\u8a72\u5167\u5bb9\u4e86\u3002<\/p>\n<h1>\u5c0f\u7d50<\/h1>\n<p>\u4eca\u5929\u6559\u7a0b\u6559\u6388\u5927\u5bb6\u5982\u4f55\u5b89\u88dd\u3001\u5efa\u7acb\u3001\u6e2c\u8a66\u81ea\u5df1\u6240\u64b0\u5beb\u7684flask-restful api\uff0c\u57fa\u672c\u4e0a\u4eca\u65e5\u7684\u6559\u7a0b\u56de\u4e0d\u65b7\u7684\u8f09\u5f80\u5f8c\u7684\u65e5\u5b50\u51fa\u73fe\u3002\u660e\u65e5\u8981\u6559\u6388\u5927\u5bb6\u5982\u4f55\u64b0\u5beb\u4e00\u500bGET\u7684\u8acb\u6c42\uff0c\u656c\u8acb\u671f\u5f85\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>flask-restful\u8d77\u624b\u5f0f \u5728\u7d93\u904eflask\u57fa\u672c\u6559\u7a0b\u3001restful\u4ee5\u53caPOSTMAN\u7684\u6559\u7a0b\u5f8c\u7d42\u65bc\u8981\u6e96\u5099 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2018\/10\/16\/2019ironman-flask-restful-day15\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u7b2c\u5341\u5c46\u9435\u4eba\u8cfd flask-restful DAY15-\u641e\u61c2\u5982\u4f55\u5efa\u7acbResource\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[162,4,5,3],"class_list":["post-455","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2018ironman","tag-flask","tag-flask-restful","tag-python"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/455","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=455"}],"version-history":[{"count":15,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":2754,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/455\/revisions\/2754"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}