繁体   English   中英

FastAPI POST - 错误 422 详细信息'': ( ( loc'':(body file msg'':field required'', type'': value_error missing)))

[英]FastAPI POST - Error 422 detail'': ( ( loc'':(body file msg'':field required'', type'': value_error missing)))

我想在后端读取这个 xlsx 文件。 当我使用 swagger 时,我明白了,但是当我在前端测试时收到错误 422 - devtools/Network detail'': ( ( loc'':(body file msg'':field required'', type'': value_error 缺失)))。

router = APIRouter()

@router.post('/')
async def upload_file(file: Uploadfile = File(...)):

  try:
    arquivo = await file.read()
    df_cambio = pd.read_excel(arquivo)
    cambio_dict = df_cambio.to_dict('index')
    
    print(cambio_dict)
    return{"file_name": file.filename}
  
  except Exception as e:
    exception_handler(e)

反应->

export defaut function Dropzone() {
const [create, {isLoading}] = usePost();

const handleSubmit = (res) => {
    create('cambios', { data:res.file }})
};
if (isLoading) { return <LoadingPageSkeleton />;}

return (

<BasicForm
  initialValues={{}}
  onSubmit={handleSubmit}
>
    <FormInput id="file />
    <FormSubmitButton/>
</Basicform>

我尝试为FastAPI 演示文件上传创建 python 弹出请求时遇到了类似的问题。 docs中工作,它以 curl 行的形式为我提供了现成的代码:

curl -X 'POST' \
  'http://127.0.0.1:8000/uploadfile/' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'file=@test_file'

(FastAPI 自动生成的文档页面)。 我用它作为提示,从中我得到了 header 文件名的形式应该是"type": "multipart/form-data", files = { "file": test_file_descriptor }并且它有效

更准确地说:文件描述符的键应该是file 我的案例(Python): test_response = requests.post(test_url, data={'filename': test_file, "msg":"hello","type": "multipart/form-data"}, files = { "file": test_file } )其中test_file是文件描述符。 我知道我的情况可能会有所不同,但这是此错误唯一有意义的谷歌链接,我的情况下的人也可以使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM