I need to send files and data through FastAPI. I'm trying to send requests but couldn't get it to work
for example :
server:
import uvicorn
from fastapi import FastAPI, File
from pydantic import BaseModel
app = FastAPI()
class Data(BaseModel):
    test1: str = None
    test2: str = None
    test3: str = None
@app.post("/uploadfile/")
async def create_file(
    data: Data,
    image: bytes = File(...),
):
    return {"postdata": len(image),
            "data": data.camera,
            }
if __name__ == "__main__":
    uvicorn.run(app)
client:
import requests
import cv2
import json
image = cv2.imread('/marti.jpg')
data2 = cv2.imencode(".jpg", image)[1]
payload = {"test1": "value_1", "test2": "value_2", "test3": "value_3"}
files = {
    'image': ('a.jpg', data2.tobytes(), 'image/jpeg', {'Expires': '0'})
}
res = requests.post("http://localhost:8000/uploadfile/",
                    files=files, data=payload)
print(res.content)
print(res.status_code, res.json())
the error i got:
422 Unprocessable Entity
- If I remove the 'files' from the request, it works.
- If I remove the 'data' from the request, it works.
What are your suggestions in the face of this situation?
 
     
    