Fastapi: [BUG] 422 Unprocessable Entity with clean inputs

Created on 15 Feb 2020  路  3Comments  路  Source: tiangolo/fastapi

Describe the bug

422 Unprocessable Entity with clean inputs when posting json

To Reproduce

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

Environment

  • OS: macOS
  • FastAPI Version ==0.48.0
bug

Most helpful comment

In your example FastAPI is expecting query params. You may want to read https://fastapi.tiangolo.com/tutorial/body/

All 3 comments

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:

Was this page helpful?
0 / 5 - 0 ratings