FastAPI выдает ошибку 422 Unprocessable Entity при загрузке файла через PostmanPython

Программы на Python
Ответить
Anonymous
 FastAPI выдает ошибку 422 Unprocessable Entity при загрузке файла через Postman

Сообщение Anonymous »

Я использую запрос POST, чтобы загрузить файл через Postman и сохранить его в своем локальном каталоге. Однако возникает ошибка 422 (Необрабатываемый объект), как показано ниже:
[img]https://i.sstatic .net/f5KJoYq6.png[/img]

Я выбрал файл как двоичный и загрузил его на Postman, как вы можете видеть на опубликованном изображении выше.
Ниже показано, как мой Серверная часть FastAPI выглядит так:
main.py
from fastapi import FastAPI
from api.endpoints.vendor import router

app = FastAPI(title='Vendor Acknolegment API')
app.include_router(router, prefix='/vendor', tags=['vendor confirmation'])

if __name__ == '__main__':
import uvicorn
print("entered here")
uvicorn.run("main:app", host="0.0.0.0", port=8000,
log_level='info', reload=True)


vendor.py
from fastapi import APIRouter, status, File, UploadFile
#from lxml import etree
import os

# file path
UPLOAD_DIR = r"c:\ack"

# check if the directory exists.
os.makedirs(UPLOAD_DIR, exist_ok=True)

# creates the endpoint path
router = APIRouter()

# POST Ack
@router.post("/ack/", status_code=status.HTTP_201_CREATED)
async def upload_ack(file: UploadFile = File(...)):
# define the complete path where the file will be saved.
file_location = os.path.join(UPLOAD_DIR, file.filename)

with open(file_location, "wb") as f:
f.write(await file.read())

return {"message": f"The file '{file.filename}' has been successfully saved into the server."}


Подробнее здесь: https://stackoverflow.com/questions/792 ... ugh-postma
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»