{"id":205148,"date":"2025-05-29T10:40:04","date_gmt":"2025-05-29T02:40:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205148\/"},"modified":"2025-05-29T10:40:04","modified_gmt":"2025-05-29T02:40:04","slug":"fastapi%e4%b8%ad%e5%a6%82%e4%bd%95%e8%87%aa%e5%ae%9a%e4%b9%89swagger%e5%93%8d%e5%ba%94%e7%9a%84%e5%aa%92%e4%bd%93%e7%b1%bb%e5%9e%8b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205148\/","title":{"rendered":"FastAPI\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49Swagger\u54cd\u5e94\u7684\u5a92\u4f53\u7c7b\u578b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>FastAPI\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49Swagger\u54cd\u5e94\u7684\u5a92\u4f53\u7c7b\u578b\uff1f <\/h1>\n<p>\u4eca\u65e5\u4e0d\u80af\u57cb\u5934\uff0c\u660e\u65e5\u4f55\u4ee5\u62ac\u5934\uff01\u6bcf\u65e5\u4e00\u53e5\u52aa\u529b\u81ea\u5df1\u7684\u8bdd\u54c8\u54c8~\u54c8\u55bd\uff0c\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\uff0c\u4e3b\u8981\u5185\u5bb9\u662f\u8bb2\u89e3<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u53ef\u4ee5\u6536\u85cf\u6216\u8005\u6709\u66f4\u597d\u7684\u5efa\u8bae\u5728\u8bc4\u8bba\u63d0\u51fa\uff0c\u6211\u90fd\u4f1a\u8ba4\u771f\u770b\u7684\uff01\u5927\u5bb6\u4e00\u8d77\u8fdb\u6b65\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/1732446405674308c572202.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>fastapi \u8bbe\u7f6e swagger response \u7684 media_type<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 fastapi \u6846\u67b6\u5f00\u53d1 restful api \u65f6\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u63a5\u53e3\u7684\u54cd\u5e94\u5185\u5bb9\u8bbe\u7f6e\u9002\u5f53\u7684\u5a92\u4f53\u7c7b\u578b\uff08media_type\uff09\uff0c\u4ee5\u4fbf\u5ba2\u6237\u7aef\u80fd\u591f\u6b63\u786e\u89e3\u6790\u548c\u5904\u7406\u54cd\u5e94\u3002\u5bf9\u4e8e\u8fd4\u56de\u56fe\u50cf\u6d41\u7684\u63a5\u53e3\uff0c\u9700\u8981\u5c06 media_type \u8bbe\u7f6e\u4e3a &#8220;image\/jpg&#8221; \u6216\u76f8\u5e94\u7684\u56fe\u50cf\u7c7b\u578b\uff0c\u4ee5\u544a\u77e5\u5ba2\u6237\u7aef\u8fd4\u56de\u7684\u5185\u5bb9\u7c7b\u578b\u3002<\/p>\n<p>\u7136\u800c\uff0c\u5bf9\u4e8e\u4f7f\u7528\u9ed8\u8ba4\u54cd\u5e94\u7c7b\u7684\u63a5\u53e3\uff08\u5982\u8fd4\u56de json \u7684\u63a5\u53e3\uff09\uff0cfastapi \u4f1a\u81ea\u52a8\u5728\u54cd\u5e94\u5934\u4e2d\u8bbe\u7f6e &#8220;content-type&#8221;\uff0c\u65e0\u9700\u663e\u5f0f\u58f0\u660e media_type\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u5bf9\u4e8e\u8fd4\u56de\u81ea\u5b9a\u4e49\u54cd\u5e94\u7c7b\u7684\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 &#8220;media_type&#8221; \u5c5e\u6027\u6765\u6307\u5b9a\u54cd\u5e94\u7684\u5a92\u4f53\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e\u81ea\u5b9a\u4e49\u54cd\u5e94\u7c7b\u7684\u5a92\u4f53\u7c7b\u578b\u4e3a &#8220;image\/jpeg&#8221;\uff1a<\/p>\n<pre>from fastapi import FastAPI\nfrom fastapi.responses import StreamingResponse\n\napp = FastAPI()\n\nclass MyCustomResponse(StreamingResponse):\n    media_type = \"image\/jpeg\"\n\n@app.get(\"\/img\", response_class=MyCustomResponse)\ndef image():\n    def iterfile():\n        with open(\".\/image.jpg\", mode=\"rb\") as file_like:\n            yield from file_like\n\n    return MyCustomResponse(iterfile())<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c&#8221;mycustomresponse&#8221; \u7c7b\u7ee7\u627f\u81ea fastapi \u63d0\u4f9b\u7684 &#8220;streamingresponse&#8221; \u7c7b\uff0c\u5e76\u91cd\u5199\u4e86 &#8220;media_type&#8221; \u5c5e\u6027\uff0c\u5c06\u5176\u8bbe\u7f6e\u4e3a &#8220;image\/jpeg&#8221;\u3002\u901a\u8fc7\u5c06 &#8220;mycustomresponse&#8221; \u4f5c\u4e3a &#8220;image&#8221; \u63a5\u53e3\u7684 &#8220;response_class&#8221;\uff0c\u53ef\u4ee5\u786e\u4fdd\u8fd4\u56de\u7684\u56fe\u50cf\u6d41\u54cd\u5e94\u5e26\u6709\u6b63\u786e\u7684\u5a92\u4f53\u7c7b\u578b\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49 swagger response \u7684 media_type\uff0c\u786e\u4fdd\u5ba2\u6237\u7aef\u80fd\u591f\u51c6\u786e\u5730\u5904\u7406\u8fd4\u56de\u7684\u5404\u79cd\u54cd\u5e94\u7c7b\u578b\u7684 api \u63a5\u53e3\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FastAPI\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49Swa&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205148","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205148"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205148\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}