{"id":33,"date":"2023-09-28T13:45:20","date_gmt":"2023-09-28T05:45:20","guid":{"rendered":"http:\/\/www.weigot.com\/?p=33"},"modified":"2023-09-28T13:45:20","modified_gmt":"2023-09-28T05:45:20","slug":"fastapi-%e6%b3%a8%e5%86%8c%e5%b9%b6%e4%b8%8e%e7%ac%ac%e4%b8%89%e6%96%b9%e8%bf%9b%e8%a1%8c%e9%9b%86%e6%88%90","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2023\/09\/28\/fastapi-%e6%b3%a8%e5%86%8c%e5%b9%b6%e4%b8%8e%e7%ac%ac%e4%b8%89%e6%96%b9%e8%bf%9b%e8%a1%8c%e9%9b%86%e6%88%90\/","title":{"rendered":"FastAPI \u6ce8\u518c\u5e76\u4e0e\u7b2c\u4e09\u65b9\u8fdb\u884c\u96c6\u6210"},"content":{"rendered":"\n<p>\u5f53\u4f7f\u7528 FastAPI \u6ce8\u518c\u5e76\u4e0e\u7b2c\u4e09\u65b9\u8fdb\u884c\u96c6\u6210\u65f6\uff0c\u4f60\u901a\u5e38\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5bfc\u5165\u6240\u9700\u7684\u6a21\u5757\u548c\u5e93\uff1a<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   from fastapi import FastAPI\n   import requests\n   ```\n\n2. \u521b\u5efa FastAPI \u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\uff1a<\/code><\/pre>\n\n\n\n<p>python<br>app = FastAPI()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>3. \u5b9a\u4e49\u4e00\u4e2a\u8def\u7531\u6765\u5904\u7406\u6ce8\u518c\u8bf7\u6c42\uff1a<\/code><\/pre>\n\n\n\n<p>`python<br>@app.post(&#8220;\/register&#8221;)<br>def register_user(user_data: dict):<br># \u5c06\u7528\u6237\u6570\u636e\u53d1\u9001\u5230\u7b2c\u4e09\u65b9\u6ce8\u518c API<br>response = requests.post(&#8220;https:\/\/third-party-api.com\/register&#8221;, json=user_data)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   # \u5904\u7406\u7b2c\u4e09\u65b9 API \u7684\u54cd\u5e94\n   if response.status_code == 200:\n       return {\"message\": \"Registration successful\"}\n   else:\n       return {\"message\": \"Registration failed\"}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>   \u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a `POST` \u7c7b\u578b\u7684\u8def\u7531 `\/register`\uff0c\u63a5\u53d7\u4e00\u4e2a\u7528\u6237\u6570\u636e\u7684 JSON \u5bf9\u8c61\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\u3002\u7136\u540e\uff0c\u4f7f\u7528 `requests` \u6a21\u5757\u5c06\u7528\u6237\u6570\u636e\u4f5c\u4e3a JSON \u53d1\u9001\u5230\u7b2c\u4e09\u65b9\u6ce8\u518c API\u3002\n\n4. \u542f\u52a8 FastAPI \u5e94\u7528\u7a0b\u5e8f\uff1a<\/code><\/pre>\n\n\n\n<p>`python<br>if <strong>name<\/strong> == &#8220;<strong>main<\/strong>&#8220;:<br>import uvicorn<br>uvicorn.run(app, host=&#8221;0.0.0.0&#8243;, port=8000)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   \u4f7f\u7528\u4ee5\u4e0a\u4ee3\u7801\uff0c\u4f60\u53ef\u4ee5\u5728\u672c\u5730\u8fd0\u884c FastAPI \u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5c06\u5176\u76d1\u542c\u5728 `0.0.0.0:8000` \u5730\u5740\u4e0a\u3002\n\n\u5f53\u4f60\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u5e76\u5411 `\/register` \u8def\u7531\u53d1\u9001 `POST` \u8bf7\u6c42\u65f6\uff0cFastAPI \u5c06\u63a5\u6536\u5230\u7684\u7528\u6237\u6570\u636e\u8f6c\u53d1\u5230\u7b2c\u4e09\u65b9\u6ce8\u518c API\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u7ed3\u679c\u3002\n\n\u8bf7\u6ce8\u610f\uff0c\u4e0a\u8ff0\u793a\u4f8b\u4ec5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u57fa\u672c\u7684\u6ce8\u518c\u793a\u4f8b\uff0c\u5e76\u4e14\u5047\u8bbe\u7b2c\u4e09\u65b9\u6ce8\u518c API \u7684\u7aef\u70b9\u4e3a `\/register`\uff0c\u5e76\u4e14\u5b83\u63a5\u53d7 JSON \u683c\u5f0f\u7684\u7528\u6237\u6570\u636e\u3002\u4f60\u9700\u8981\u6839\u636e\u4f60\u5b9e\u9645\u4f7f\u7528\u7684\u7b2c\u4e09\u65b9 API \u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u548c\u914d\u7f6e\u3002\n\n\u53e6\u5916\uff0c\u786e\u4fdd\u5728\u9879\u76ee\u4e2d\u5b89\u88c5\u4e86 FastAPI \u548c requests \u6a21\u5757\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/code><\/pre>\n\n\n\n<p>pip install fastapi<br>pip install requests<br>&#8220;`<\/p>\n\n\n\n<p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u7b2c\u4e09\u65b9 API \u7684\u8981\u6c42\u8fdb\u884c\u66f4\u591a\u7684\u5b9a\u5236\u548c\u6269\u5c55\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f7f\u7528 FastAPI \u6ce8\u518c\u5e76\u4e0e\u7b2c\u4e09\u65b9\u8fdb\u884c\u96c6\u6210\u65f6\uff0c\u4f60\u901a\u5e38\u9700\u8981\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a pythonapp = Fas [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":1,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":34,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions\/34"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}