[Python] Getting a 422 error response when calling FastAPI backend with a GET request using...

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a GET request from my Vue3 client (using axios JavaScript HTTP client) to a FastAPI server, but I´m getting a 422 unprocessable entity error response. As I understand, this kind of error messages are related to a bad input.

    This is the vue3 code:

    import { authToken } from "../main.js"

    export default {
    ...
    mounted() {
    const response = axios.get('/checkIsLogged', authToken);
    console.log("authToken" + authToken)
    this.isLoggedIn = response
    console.log("isLoggedin", response)
    if (response) {
    this.title3 = "Mi perfil"
    } else {
    this.title1 = "Sign in"
    }
    },


    where authToken:

    export const authToken = ""

    export const setAuthToken = (token) => {
    authToken = token
    }



    On the other side, in FastAPI backend, the endpoint looks like this:

    @empresa.get("/checkIsLogged", response_model=bool)
    def is_token_expired(authToken: str):
    try:
    payload = jwt.decode(authToken, SECRET_KEY, algorithms=[ALGORITHM])
    expiration_timestamp = payload.get("exp")
    if expiration_timestamp is None:
    return True
    expiration_datetime = datetime.fromtimestamp(expiration_timestamp)
    current_datetime = datetime.now()
    if current_datetime >= expiration_datetime:
    return True
    return False
    except JWTError:
    return False



    What am I doing wrong?

    Thanks in advance.

    Continue reading...

Compartilhe esta Página