【问题标题】:how to route from one api to other fastapi如何从一个 api 路由到另一个 fastapi
【发布时间】:2022-02-04 08:27:33
【问题描述】:

我正在尝试将请求从 protected_api() 重定向到 fastapi 中的 login() 函数。但它失败了消息

获取失败。 可能的原因: CORS 网络故障 对于 CORS 请求,URL 方案必须是“http”或“https”。

可能是什么问题以及如何从一个 api 重定向到另一个 api

@app.get("/protected_api")
async def protected_api():    
    resp = RedirectResponse("https://localhost:5000/token")
    return resp
    
@app.post("/token", response_model=Token)
async def login(form_data: OAuth2PasswordRequestForm = Depends()):  # login function to get access token
    print('In login fun value of form_data dict.....%s' % form_data.__dict__)
    user = authenticate_user(fake_users_db, form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(seconds=ACCESS_TOKEN_EXPIRE_SECONDS)
    access_token = create_access_token(
        data={"sub": user.username}, expires_delta=access_token_expires
    )
    print('Value of access_token in login fun ......%s\n' % access_token)
    return {"access_token": access_token, "token_type": "bearer"}   

【问题讨论】:

  • 您需要提供更多信息 - 因为它没有 httphttps 作为协议,所以错误输出的请求到底是什么样的?

标签: fastapi


【解决方案1】:

您让客户端 ping A (/protected_api) 但从 B (/token) 发送响应。它们不是同一个端点,所以这涉及到CORS。尝试使用 CORSMiddleware 和 FastAPI 来解决此问题。

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    相关资源
    最近更新 更多