我正在創建一個需要登錄驗證的 Flutter Web 應用程式。用戶使用身份驗證資訊發出發布請求,然后我的 Flask 應用程式將 cookie 發送回客戶端。
這是 Flask 應用程式的代碼
@app.route('/test', methods=['POST'])
@cross_origin(supports_credentials=True)
def test():
resp = jsonify({'message' : 'Logged in!'})
resp.set_cookie('Set-Cookie', "token", httponly = True, secure = False)
return resp
這是我發出 POST 請求并期望一個名為“Set-Cookie”的 cookie 的 Dart/Flutter 代碼。
class HttpService {
static var dio = Dio();
static testMethod() async {
try {
dio.options.extra['withCredentials'] = true;
var response = await dio.post('http://127.0.0.1:5000/test');
print(response);
} catch (e) {
print(e);
}
}
如您所見,我的瀏覽器沒有收到這個 cookie,但是請求成功并且我收到了 JSON 訊息!

但是,當我在 Postman 上發出同樣的請求時,我得到 JSON 回應和 cookie。
任何幫助將不勝感激!如果您需要更多詳細資訊/代碼,請告訴我。
uj5u.com熱心網友回復:
感謝Kris,我意識到我是從 Flutter(客戶端)向 IP 而不是域名localhost發出請求。因為設定 cookie 是特定于域的,所以我無法在開發人員控制臺中看到設定的 cookie。
這是更新的代碼
static testMethod() async {
try {
dio.options.extra['withCredentials'] = true;
var response = await dio.post('http://localhost:5000/test');
print(response);
} catch (e) {
print(e);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463575.html
