I am trying to upload a multipart file to a FastAPI server using OpenAPI to share the types for the upload.
This is my client:
import type { paths } from "./apiTypes";
import createClient from "openapi-fetch";
export let { GET, POST, PATCH, PUT, DELETE, HEAD, TRACE } = createClient<paths>(
{
baseUrl: "http://localhost:8000/",
}
);
This is my upload file definition:
load_file_load_post: {
requestBody: {
content: {
"multipart/form-data": components["schemas"]["Body_load_file_load_post"];
};
};
responses: {...
Uploading from JS (the problem is here probably):
// formData is new FormData()
await POST('/load', {
body: formData
})
FastAPI:
@app.post("/load")
async def load_file(file: UploadFile = File(...), db: Session = Depends(get_db)):
try:
file_content = await file.read()
For some reason FastAPI still says that I’m missing fileds (file
).