[Python] FastAPI is not returning cookies to React frontend

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo

    Why doesn't FastAPI return the cookie to my frontend, which is a React app?

    Here is my code:

    @router.post("/login")
    def user_login(response: Response,username :str = Form(),password :str = Form(),db: Session = Depends(get_db)):
    user = db.query(models.User).filter(models.User.mobile_number==username).first()
    if not user:
    raise HTTPException(400, detail='wrong phone number or password')
    if not verify_password(password, user.password):
    raise HTTPException(400, detail='wrong phone number or password')


    access_token = create_access_token(data={"sub": user.mobile_number})
    response.set_cookie(key="fakesession", value="fake-cookie-session-value") #here I am set cookie
    return {"status":"success"}


    When I login from Swagger UI autodocs, I can see the cookie in the response headers using DevTools on Chrome browser. However, when I login from my React app, no cookie is returned. I am using axios to send the request like this:

    await axios.post(login_url, formdata)

    Continue reading...

Compartilhe esta Página