When I use a POST request to send form data from a JS script running on port 5500 using XMLHttpRequest to my backend which is a FastAPI app running on port 8000, I am always met with this error Unprocessable Entity.
Here is the server-side:
origins = ["https://localhost:5500"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/test")
def test(username: str = Form(...)):
print(f"########### Received {username} #############")
And here is the client-side code:
async function sendData() {
formData = new FormData();
formData.append('username', 'Kareem');
const response = await fetch('http://localhost:8000/test', {
method: 'POST',
data: formData
})
}