422 Unprocessable Entity with clean inputs when posting json
Steps to reproduce the behavior with a minimum self-contained file.
On the server side:
@router.post("/join")
async def login_user(x:str, y:str):
return {"joined": f"{x}-{y}"}
On the client side (js)
const req = request('/api/join', {
method: 'POST',
data: {"x":"x", "y":"y"},
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
}
});
I get this error:
422 Unprocessable Entity
I've done some debugging on the server side -headers have json defined:
'content-type': 'application/json'
'accept': 'application/json'
And weirdly enough - if I destructure the request myself I can see the values being passed in fine:
@router.post("/join")
async def login_user(x:str, y:str):
data = await request.json() #<< This is parsed correctly
print(data)
return {"joined": f"{x}-{y}"}
In your example FastAPI is expecting query params. You may want to read https://fastapi.tiangolo.com/tutorial/body/
Got it - thank you!
Thanks for the help here @phy25 ! :cake: :bowing_man:
Thanks @eliehamouche for reporting back and closing the issue :+1:
Most helpful comment
In your example FastAPI is expecting query params. You may want to read https://fastapi.tiangolo.com/tutorial/body/