Wednesday, 5 October 2022

API responses with 422 on login request

I want to login a user via a form and a API request. But I allways get a 422 Error, and I can't locate the faulty code.

The API is written with FastAPI in python. This is the Swagger API documentation.

When I try to login via Swagger it works and I get a 200.

FastAPI written in Python

@router.post("/login", status_code=status.HTTP_200_OK, response_model=Token)
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(),
                             db: Session = Depends(get_db)):
user = authenticate_user(form_data.username, form_data.password, db)
if not user:
    raise HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Incorrect username or password",
        headers={"WWW-Authenticate": "Bearer"},
    )

access_token_expires = timedelta(minutes=60)
access_token = create_access_token(
    data={"sub": user.username}, expires_delta=access_token_expires
)
check_password_expiration(user)
if user.change_password:
    return {"access_token": access_token,
            "token_type": "bearer",
            "message": "Password expired, please set new password"}
return {"access_token": access_token, "token_type": "bearer"}

Javascript API Request

function loginRequest(username_value, password_value) {
var data = new FormData();
data.append("username", username_value);
data.append("password", password_value);

for (var pair of data.entries()) {
    console.log(pair[0] + ', ' + pair[1]);
}

fetch("https://www.example-url.net", {
    method: "POST",
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: data
})
.then(response => {
    console.log(response.json())
})
}

With that request, I get a 422 Error.

If I take a look into the console I get the body

enter image description here

At which Point is the server not able to process the request body? Why are the values not correctly processed?

PS: I know that I get a jwt in return and I'm not processing that one yet. But in the first step I would like to make a successfull API Call.



from API responses with 422 on login request

No comments:

Post a Comment