简体   繁体   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)))

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.

相关问题 POST 请求响应 422 错误 {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} - POST request response 422 error {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} 如果我进行查询,为什么会得到 {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]}与参数? - Why am I getting {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]} if I made query with params? python: {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} - python : {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} 嵌套 Pydantic Model 使用 FastAPI 返回错误:需要字段(type=value_error.missing) - Nested Pydantic Model return error with FastAPI : field required (type=value_error.missing) FastAPI - 必填字段,缺少值 - FastAPI - Field required, missing value 使用带有 FastAPI 的 POST 时缺少值错误 - Value Error Missing when using POST with FastAPI 使用 FastAPI 上传文件返回错误 422 - File upload using FastAPI returns error 422 wordpress:wp_remote_post 无法将主体传递给快速 api,而来自 api 文档和 python 请求没有问题,错误消息:值不是有效的字典,422 - wordpress: wp_remote_post can't pass body to the fast api, while from api docs and python requests no issue, error msg: value is not a valid dict, 422 带有 List 输入的 FastAPI POST 请求引发 422 Unprocessable Entity 错误 - FastAPI POST request with List input raises 422 Unprocessable Entity error Python:发送 JSON 数据时,FastAPI 错误 422 与 POST 请求 - Python: FastAPI error 422 with POST request when sending JSON data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM