[英]FastAPI POST - Error 422 detail'': ( ( loc'':(body file msg'':field required'', type'': value_error missing)))
I want to read in backend this xlsx file.我想在后端读取这个 xlsx 文件。 When I use the swagger I get it, but when I test in frontend I receive an Error 422 - devtools/Network detail'': ( ( loc'':(body file msg'':field required'', type'': value_error missing))).
当我使用 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)
react ->反应->
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>
I faced similar issue by trying to create python pop request for FastAPI demo file upload .我尝试为FastAPI 演示文件上传创建 python 弹出请求时遇到了类似的问题。 Working from
docs
where it gave me ready code in form of curl line:从
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 auto generated docs page). (FastAPI 自动生成的文档页面)。 Which i used as hint and from which I got that header for file name should be in the form of
"type": "multipart/form-data", files = { "file": test_file_descriptor }
And it worked我用它作为提示,从中我得到了 header 文件名的形式应该是
"type": "multipart/form-data", files = { "file": test_file_descriptor }
并且它有效
More exact: key for the file descriptor should be file
.更准确地说:文件描述符的键应该是
file
。 My case (Python): test_response = requests.post(test_url, data={'filename': test_file, "msg":"hello","type": "multipart/form-data"}, files = { "file": test_file } )
Where test_file
is file descriptor.我的案例(Python):
test_response = requests.post(test_url, data={'filename': test_file, "msg":"hello","type": "multipart/form-data"}, files = { "file": test_file } )
其中test_file
是文件描述符。 I know that my case might be different, but this is the only meaningful google link for this error, and someone with my case could also use it.我知道我的情况可能会有所不同,但这是此错误唯一有意义的谷歌链接,我的情况下的人也可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.