How to send credentials as JSON body from Swagger form in FastAPI?

Viewed 35

I'm learning FastAPI. I am considering whether it is possible to send an email and password (credentials data) from Swagger UI as json data in the authentication form?

I defined authentiaction functions:

# ...
class Login(BaseModel):
    email: str
    password: str

@router.post('/login')
def login(request: Login, db: Session = Depends(get_db)):
    user = db.query(models.User).filter(models.User.email == request.email).first()
    if not user:
    # ...

It works fine when I send requests with curl:

> curl -X 'POST' \
  'http://127.0.0.1:5000/login' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "email": "johny@gmail.com",
  "password": "abc"
}'

{
  "access_token": "...",
  "token_type": "bearer"
}

And:

> curl -X 'GET' \
  -H 'Content-Type: application/json' -H 'Authorization: Bearer eyJ...hbG' \ 
  http://127.0.0.1:5000/blog/

[]

But it doesn't work from Swagger docs (image of swagger form):

INFO: 127.0.0.1:8507 - "POST /login HTTP/1.1" 422 Unprocessable Entity

Do you have any guides how to configure swagger to send this data as json structure?

0 Answers
Related