FastAPI POST - 错误 422 详细信息'': ( ( loc'':(正文文件 msg'': 必填字段'',类型 '': value_error 缺失)))

问题描述 投票:0回答:2

我想在后端读取这个 xlsx 文件。当我使用 swagger 时,我得到了它,但是当我在前端测试时,我收到错误 422 - devtools/网络详细信息'': ( ( 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>
python typescript forms post fastapi
2个回答
3
投票

我在尝试为 FastAPI 演示文件上传创建 python pop 请求时遇到了类似的问题。从

docs
开始工作,它以卷曲线的形式为我提供了准备好的代码:

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 自动生成的文档页面)。 我用它作为提示,从中我得到文件名的标头应该采用

"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` 是文件描述符。

我知道我的情况可能有所不同,但这是此错误唯一有意义的谷歌链接,有我情况的人也可以使用它。


0
投票

检查您发送的发布请求,因为它区分大小写。swagger 文档发布部分和测试文档发布请求应该匹配。

And request 
"""{
            "question": "#(question)",
            "History":{}
                   
    }"""    
When method post
© www.soinside.com 2019 - 2024. All rights reserved.