![](/img/trans.png)
[英]POST request response 422 error {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]}
[英]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.