{"id":8934,"date":"2023-09-13T01:38:23","date_gmt":"2023-09-12T17:38:23","guid":{"rendered":"\/?p=8934"},"modified":"2023-09-13T01:38:23","modified_gmt":"2023-09-12T17:38:23","slug":"fastapi%e5%9f%ba%e7%a1%80%ef%bc%9a3-%e8%af%b7%e6%b1%82%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"\/?p=8934","title":{"rendered":"FastAPI\u57fa\u7840\uff1a3.\u8bf7\u6c42\u53c2\u6570"},"content":{"rendered":"<h2>\u8def\u5f84\u53c2\u6570 Path<\/h2>\n<p>ps: \u901a\u5e38\u4e00\u4e2aurl\u4e2d\uff0c\u53ef\u53d8\u7684\u90a3\u90e8\u5206\u6570\u636e\uff1b\u5373\u8bbf\u95ee\u8005\u4f20\u9012\u7684\u65f6\u5019\u5728url\u4e2d\u6307\u5b9a<\/p>\n<p>\u9700\u8981{\u53c2\u6570} \u548c \u89c6\u56fe\u4e2d\u7684\u53c2\u6570\u8fdb\u884c\u6620\u5c04\uff0c\u5982\u679c\u6ca1\u6709\u5c31\u662f\u666e\u901a\u8def\u5f84<\/p>\n<pre><code class=\"language-python\">@app.get(&quot;\/get\/{id}&quot;)  # {id} \u4e3a\u8def\u5f84\u53c2\u6570\uff0c\u7531\u8bbf\u95ee\u8005\u51b3\u5b9a\u6700\u7ec8\u8bbf\u95ee\u7684\u8def\u7531 host:port\/get\/1  or host:port\/get\/777\nasync def get_id(id: int): # \u51b3\u5b9a\u8def\u7531\u53c2\u6570\u7684\u7c7b\u578b \uff0c\u81ea\u52a8\u5904\u7406\u6210int \u7c7b\u578b\uff0c\u5982\u679c\u80fd\u6210\u529f\u7684\u5316\uff0c \u6b64\u5904\u53c2\u6570\u540d\u79f0\u9700\u8981\u548c \u8def\u5f84\u53c2\u6570\u5bf9\u5e94\n    return id\n\n# \u66f4\u591a\u5b9a\u4e49 \u8def\u5f84\u53c2\u6570 Path\n&quot;&quot;&quot;\ndefault \u9ed8\u8ba4\u503c \u7b49\u4ef7\u4e8e id: int = 10\ngt\uff1a\u5927\u4e8e\uff08greater than\uff09\nge\uff1a\u5927\u4e8e\u7b49\u4e8e\uff08greater than or equal\uff09\nlt\uff1a\u5c0f\u4e8e\uff08less than\uff09\nle\uff1a\u5c0f\u4e8e\u7b49\u4e8e\uff08less than or equal\uff09\nincluede_in_schema\uff1a\u663e\u793a\u63a5\u53e3\u6587\u6863\nexample\uff1a\u663e\u793a\u63a5\u53e3\u6587\u6863\u4e2d\nmax_length\uff1a\u957f\u5ea6\nregx\uff1a\u6b63\u5219\u8868\u8fbe\u5f0f\n&quot;&quot;&quot;\nfrom fastapi import Path\n@app.get(&quot;\/get\/path\/{id}&quot;) \nasync def get_pid(id: int = Path(default=10, description=&quot;ad&quot;, gt=11, example=5)):\n    return id<\/code><\/pre>\n<h2>\u67e5\u8be2\u53c2\u6570 Query<\/h2>\n<p>ps: \u901a\u5e38\u7684url\u5f62\u5f0f\u4e3a<code> 127.0.0.1:8000\/query?name=777&amp;age=16<\/code> <strong>?<\/strong>\u540e\u9762\u65e2\u662f\u53c2\u6570\u7684\u5f00\u59cb &amp;\u4e3a\u591a\u4e2a\u53c2\u6570\u7684\u94fe\u63a5 \u5982\u679c\u65e0\u8def\u5f84\u53c2\u6570\u6620\u5c04\uff0c\u65e0Body\u3001Form\u6807\u8bc6 \u5219\u9ed8\u8ba4\u662f \u67e5\u8be2\u53c2\u6570<\/p>\n<pre><code class=\"language-Python\">&quot;&quot;&quot;\n\u67e5\u8be2\u53c2\u6570\uff1a\u5373url - 127.0.0.1:8000\/query?name=777&amp;age=16\n&quot;&quot;&quot;\n@app.get(&quot;\/query&quot;)\nasync def query(name: str, age: int = 18):\n    return name, age\n\n&quot;&quot;&quot;\n\u66f4\u591a\u5b9a\u4e49\uff0c\u53c2\u6570\u548cPath\u7c7b\u4f3c\n&quot;&quot;&quot;\nfrom fastapi import Query\n\n@app.get(&quot;\/query1&quot;)\nasync def query1(name: str = Query(&quot;777&quot;, alias=&quot;\u522b\u540d 7y&quot;, title=&quot;\u6211\u7684title&quot;)):\n    return name<\/code><\/pre>\n<h2>\u8bf7\u6c42\u4f53\u53c2\u6570 Body<\/h2>\n<p>ps: request header\u4e2d content-type\uff1aapplication\/json; <\/p>\n<p>\u5982\u679c\u53c2\u6570\u662f Dict\u7c7b\u578b\u6216\u8005 Pydnatic Model \u7c7b\u578b\u5219\u88ab\u81ea\u52a8\u5904\u7406\u6210\u8bf7\u6c42\u4f53\u53c2\u6570<\/p>\n<pre><code class=\"language-python\">&quot;&quot;&quot;\nBody \u53c2\u6570 -\u8bf7\u6c42\u4f53 Json\nContent-Type: application\/json  request header\u4e2d\u6307\u5b9a\n&quot;&quot;&quot;\nfrom typing import Dict\n# 1. \u4f7f\u7528\u5b57\u5178 \u4e0d\u5e38\u7528\n@app.post(&quot;\/json&quot;)\nasync def json_data(info: Dict[str, str] ={}):\n    return info\n\n# 2. Body \u53ef\u7528\u53c2\u6570\u548cPath \u7c7b\u4f3c\nfrom fastapi import Body\n@app.post(&quot;\/body&quot;)\nasync def body(name: str = Body(max_length=4), age: int=Body()):\n    return name,age\n\n# 3. pydantic\u6a21\u578b\nfrom pydantic import BaseModel, Field\nfrom typing import Optional\n\nclass Info(BaseModel):\n    name: Optional[str]\n    # \u53ef\u4ee5\u901a\u8fc7Field\u6765\u5b9a\u4e49\u66f4\u591a\n    age: int = Field(gt=18)\n\n&quot;&quot;&quot;\n{&quot;name&quot;:&quot;xx&quot;, &quot;age&quot;:18} \n&quot;&quot;&quot;\n@app.post(&quot;\/pydantic&quot;)\nasync def pydantic(info: Info):\n    return info\n\n# 4. \u914d\u5408body\n&quot;&quot;&quot;\n\u5c06body\u53c2\u6570 loves \u52a0\u5230json \u4f53 info \u4e2d\n&quot;&quot;&quot;\n{\n    &quot;info&quot;: {&quot;name&quot;:&quot;xxx&quot;,&quot;age&quot;:18},\n    &quot;loves&quot;:&quot;xxx&quot;\n}\n\n@app.post(&quot;\/pb&quot;)\nasync def pb(info: Info, loves: str = Body()):\n    return info\n<\/code><\/pre>\n<h2>\u8868\u5355\u53c2\u6570 Form \uff08\u7528\u7684\u8f83\u5c11\uff09<\/h2>\n<p>ps: request header Content-Type: application\/x-www-form-urlencoded<\/p>\n<pre><code class=\"language-Python\">&quot;&quot;&quot;\n\u8868\u5355\u53c2\u6570 Form \u53c2\u6570\u5b57\u6bb5\u548cPath\u7c7b\u4f3c\npip install python-multipart\ndefault = None \u975e\u5fc5\u586b\n\nrequest header Content-Type: application\/x-www-form-urlencoded\n&quot;&quot;&quot;\nfrom fastapi import Form\n\n@app.post(&quot;\/login\/&quot;)\nasync def login(username: str = Form(default=None), password: str = Form(max_length=20)):\n    return {&quot;username&quot;: username}<\/code><\/pre>\n<h2>\u4e0a\u4f20\u6587\u4ef6\uff08Form\uff09<\/h2>\n<p>\u4e0d\u5305\u542b\u6587\u4ef6\u65f6\uff0c\u8868\u5355\u6570\u636e\u4e00\u822c\u7528 application\/x-www-form-urlencoded\u300c\u5a92\u4f53\u7c7b\u578b\u300d\u7f16\u7801\u3002 \u4f46\u8868\u5355\u5305\u542b\u6587\u4ef6\u65f6\uff0c\u7f16\u7801\u4e3a multipart\/form-data\u3002<\/p>\n<pre><code class=\"language-Python\">&quot;&quot;&quot;\n\u4e0a\u4f20\u6587\u4ef6\n&quot;&quot;&quot;\nfrom fastapi import UploadFile, File\n\n&quot;&quot;&quot;\nUploadFile \u7684\u5c5e\u6027\u5982\u4e0b\uff1a\n\nfilename\uff1a\u4e0a\u4f20\u6587\u4ef6\u540d\u5b57\u7b26\u4e32\uff08str\uff09\uff0c\u4f8b\u5982\uff0c myimage.jpg\uff1b\ncontent_type\uff1a\u5185\u5bb9\u7c7b\u578b\uff08MIME \u7c7b\u578b \/ \u5a92\u4f53\u7c7b\u578b\uff09\u5b57\u7b26\u4e32\uff08str\uff09\uff0c\u4f8b\u5982\uff0cimage\/jpeg\uff1b\nfile\uff1a SpooledTemporaryFile\uff08 file-like \u5bf9\u8c61\uff09\u3002\u5176\u5b9e\u5c31\u662f Python\u6587\u4ef6\uff0c\u53ef\u76f4\u63a5\u4f20\u9012\u7ed9\u5176\u4ed6\u9884\u671f file-like \u5bf9\u8c61\u7684\u51fd\u6570\u6216\u652f\u6301\u5e93\u3002\nUploadFile \u652f\u6301\u4ee5\u4e0b async \u65b9\u6cd5\uff0c\uff08\u4f7f\u7528\u5185\u90e8 SpooledTemporaryFile\uff09\u53ef\u8c03\u7528\u76f8\u5e94\u7684\u6587\u4ef6\u65b9\u6cd5\u3002\n\nwrite(data)\uff1a\u628a data \uff08str \u6216 bytes\uff09\u5199\u5165\u6587\u4ef6\uff1b\nread(size)\uff1a\u6309\u6307\u5b9a\u6570\u91cf\u7684\u5b57\u8282\u6216\u5b57\u7b26\uff08size (int)\uff09\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1b\nseek(offset)\uff1a\u79fb\u52a8\u81f3\u6587\u4ef6 offset \uff08int\uff09\u5b57\u8282\u5904\u7684\u4f4d\u7f6e\uff1b\n\u4f8b\u5982\uff0cawait myfile.seek(0) \u79fb\u52a8\u5230\u6587\u4ef6\u5f00\u5934\uff1b\n\u6267\u884c await myfile.read() \u540e\uff0c\u9700\u518d\u6b21\u8bfb\u53d6\u5df2\u8bfb\u53d6\u5185\u5bb9\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u597d\u7528\uff1b\nclose()\uff1a\u5173\u95ed\u6587\u4ef6\u3002\n\u56e0\u4e3a\u4e0a\u8ff0\u65b9\u6cd5\u90fd\u662f async \u65b9\u6cd5\uff0c\u8981\u642d\u914d\u300cawait\u300d\u4f7f\u7528\u3002\n&quot;&quot;&quot;\n\n# file \u4e3a\u53c2\u6570\u540d\n@app.post(&quot;\/uploadfile\/&quot;)\nasync def create_upload_file(file: UploadFile):\n    return {&quot;filename&quot;: file.filename}\n\n# \u589e\u5f3a\u5b9a\u4e49 \u548c Path\u7c7b\u4f3c\n@app.post(&quot;\/upload&quot;)\nasync def upload(file: UploadFile = File(description=&quot;\u53c2\u6570\u540d\u662fexcel&quot;)):\n    return file.filename<\/code><\/pre>\n<h2>\u6df7\u5408\uff08\u8def\u5f84 + \u67e5\u8be2 + \u8868\u5355 + \u6587\u4ef6\u4e0a\u4f20\uff09<\/h2>\n<pre><code class=\"language-Python\">&quot;&quot;&quot;\n\u8bf7\u6c42\u4f53 \u540c\u65f6\u53ea\u80fd\u4f7f\u7528\u4e00\u79cd \u5373 json \u548c form \u53ea\u80fd\u540c\u65f6\u5b58\u5728\u4e00\u79cd\n&quot;&quot;&quot;\nfrom typing import List\n\n@app.post(&quot;\/example\/{name}\/123\/{age}&quot;)\nasync def example(name: str, loves: str, addr: str = Form(), pooto: List[UploadFile] = File()):\n    return name, loves, addr<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640.png\" alt=\"\u56fe\u7247\" \/>image.png<\/p>\n<h3>Requests \u8bf7\u6c42<\/h3>\n<pre><code class=\"language-python\">from requests import post\n\n# \u8def\u5f84\u53c2\u6570\npath_name = &quot;777&quot;\n# \u67e5\u8be2\u53c2\u6570\nparams = {&quot;loves&quot;: &quot;my loves&quot;}\n\n# \u8868\u5355\u53c2\u6570\ndata = {&quot;addr&quot;: &quot;wode shuju&quot;}\n\n# \u6587\u4ef6\u53c2\u6570 \u4e0a\u4f20\u591a\u4e2a\u6587\u4ef6\nfile_var = [\n    (&#039;pooto&#039;, open(&#039;main.py&#039;, &#039;rb&#039;)),\n    (&#039;pooto&#039;, open(&#039;demo.py&#039;, &#039;rb&#039;))\n]\n# \u4e0a\u4f20\u5355\u4e2a\u6587\u4ef6\nfile_var = {&#039;pooto&#039;: open(&#039;demo.py&#039;, &#039;rb&#039;)}\n\nres = post(url=&quot;http:\/\/localhost:8000\/example\/&quot; + path_name + &quot;\/123\/{age}&quot;,\n           params=params, data=data, files=file_var)\nprint(res.json())<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8def\u5f84\u53c2\u6570 Path ps: \u901a\u5e38\u4e00\u4e2aurl\u4e2d\uff0c\u53ef\u53d8\u7684\u90a3\u90e8\u5206\u6570\u636e\uff1b\u5373\u8bbf\u95ee\u8005\u4f20\u9012\u7684\u65f6\u5019\u5728url\u4e2d\u6307\u5b9a \u9700\u8981{\u53c2\u6570} \u548c \u89c6\u56fe\u4e2d\u7684\u53c2\u6570\u8fdb\u884c\u6620\u5c04   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[204],"tags":[116],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8934"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8934"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8934\/revisions"}],"predecessor-version":[{"id":8935,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8934\/revisions\/8935"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8934"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}