FastAPI löst beim Hochladen der Datei über Postman den Fehler „422 Unprocessable Entity“ ausPython

Python-Programme
Guest
 FastAPI löst beim Hochladen der Datei über Postman den Fehler „422 Unprocessable Entity“ aus

Post by Guest »

Ich verwende eine POST-Anfrage, um eine Datei über Postman in eine FastAPI-Anwendung hochzuladen und sie in meinem lokalen Verzeichnis zu speichern. Es wird jedoch der Fehler 422 (Unverarbeitbare Entität) ausgelöst, der besagt, dass die Datei fehlt. Ich habe die Binäroption ausgewählt, um die Datei hochzuladen, wie im Bild unten zu sehen ist:
[img]https:/ /i.sstatic.net/f5KJoYq6.png[/img]

So sieht mein FastAPI-Backend aus:
main.py

Code: Select all

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

Code: Select all

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."}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post