<legend id='abOIW'><style id='abOIW'><dir id='abOIW'><q id='abOIW'></q></dir></style></legend>
  • <tfoot id='abOIW'></tfoot>
    1. <small id='abOIW'></small><noframes id='abOIW'>

        <i id='abOIW'><tr id='abOIW'><dt id='abOIW'><q id='abOIW'><span id='abOIW'><b id='abOIW'><form id='abOIW'><ins id='abOIW'></ins><ul id='abOIW'></ul><sub id='abOIW'></sub></form><legend id='abOIW'></legend><bdo id='abOIW'><pre id='abOIW'><center id='abOIW'></center></pre></bdo></b><th id='abOIW'></th></span></q></dt></tr></i><div id='abOIW'><tfoot id='abOIW'></tfoot><dl id='abOIW'><fieldset id='abOIW'></fieldset></dl></div>

          <bdo id='abOIW'></bdo><ul id='abOIW'></ul>
      1. 如何使用邮递员将文件发送到 fastapi 端点

        How to send file to fastapi endpoint using postman(如何使用邮递员将文件发送到 fastapi 端点)

        • <bdo id='K0BBz'></bdo><ul id='K0BBz'></ul>
              <tbody id='K0BBz'></tbody>
            • <tfoot id='K0BBz'></tfoot>
                <legend id='K0BBz'><style id='K0BBz'><dir id='K0BBz'><q id='K0BBz'></q></dir></style></legend>

                <small id='K0BBz'></small><noframes id='K0BBz'>

                  <i id='K0BBz'><tr id='K0BBz'><dt id='K0BBz'><q id='K0BBz'><span id='K0BBz'><b id='K0BBz'><form id='K0BBz'><ins id='K0BBz'></ins><ul id='K0BBz'></ul><sub id='K0BBz'></sub></form><legend id='K0BBz'></legend><bdo id='K0BBz'><pre id='K0BBz'><center id='K0BBz'></center></pre></bdo></b><th id='K0BBz'></th></span></q></dt></tr></i><div id='K0BBz'><tfoot id='K0BBz'></tfoot><dl id='K0BBz'><fieldset id='K0BBz'></fieldset></dl></div>
                  本文介绍了如何使用邮递员将文件发送到 fastapi 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我遇到了使用 postman 测试 api 的困难.通过 swagger 文件上传功能正常工作,我在硬盘上保存了一个文件.我想了解如何与邮递员一起执行此操作.我使用标准方式来处理我在使用 Django、flask 时使用的文件.

                  I faced the difficulty of testing api using postman. Through swagger file upload functionality works correctly, I get a saved file on my hard disk. I would like to understand how to do this with the postman. I use the standard way to work with files which I use when working with Django, flask.

                  Body -> form-data: key=file, value=image.jpeg
                  

                  但是使用 fast API,我得到一个错误

                  But with fast API, I get an error

                  127.0.0.1:54294 - "POST /uploadfile/ HTTP/1.1" 422 Unprocessable Entity
                  

                  main.py

                  @app.post("/uploadfile/")
                  async def create_upload_file(file: UploadFile = File(...)):
                      img = await file.read()
                      if file.content_type not in ['image/jpeg', 'image/png']:
                          raise HTTPException(status_code=406, detail="Please upload only .jpeg files")
                      async with aiofiles.open(f"{file.filename}", "wb") as f:
                          await f.write(img)
                      return {"filename": file.filename}
                  

                  我也试过 body ->二进制:image.jpeg .但得到了相同的结果

                  I also tried body -> binary: image.jpeg . But got the same result

                  推荐答案

                  我的代码:

                  from fastapi import FastAPI, UploadFile, File
                  
                  app = FastAPI()
                  
                  @app.post("/file/")
                  async def create_upload_file(file: UploadFile = File(...)):
                      return {"filename": file.filename}
                  

                  在 Postman 中设置

                  Setup in Postman

                  如 https://github.com/tiangolo/fastapi/issues/1653,文件的参数名称是您必须使用的键值.在您使用 key=file 和 value=image.png (或其他)之前.相反,FastAPI 接受 file=image.png.因此错误,因为该文件是必需的,但它不存在(至少,具有该名称的键不存在).

                  As stated in https://github.com/tiangolo/fastapi/issues/1653, the parameter name for the file is the key value that you have to use. Before you were using key=file and value=image.png (or whatever). Instead, FastAPI accepts file=image.png. Thus the error, since the file is necessary, but it is not present (at least, the key with that name is not present).

                  附:我用 Postman v7.16.1 测试过

                  P.S. I tested it with Postman v7.16.1

                  这篇关于如何使用邮递员将文件发送到 fastapi 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                  <small id='7P1dR'></small><noframes id='7P1dR'>

                  • <i id='7P1dR'><tr id='7P1dR'><dt id='7P1dR'><q id='7P1dR'><span id='7P1dR'><b id='7P1dR'><form id='7P1dR'><ins id='7P1dR'></ins><ul id='7P1dR'></ul><sub id='7P1dR'></sub></form><legend id='7P1dR'></legend><bdo id='7P1dR'><pre id='7P1dR'><center id='7P1dR'></center></pre></bdo></b><th id='7P1dR'></th></span></q></dt></tr></i><div id='7P1dR'><tfoot id='7P1dR'></tfoot><dl id='7P1dR'><fieldset id='7P1dR'></fieldset></dl></div>
                      <tbody id='7P1dR'></tbody>
                      <legend id='7P1dR'><style id='7P1dR'><dir id='7P1dR'><q id='7P1dR'></q></dir></style></legend>

                      <tfoot id='7P1dR'></tfoot>
                          • <bdo id='7P1dR'></bdo><ul id='7P1dR'></ul>