为什么我会看到我从烧瓶中发送的饼干到浏览器中的颤动?

发布于 2025-01-22 09:43:45 字数 1018 浏览 2 评论 0原文

我正在创建一个需要登录验证的Flutter Web应用程序。用户将提供带有身份验证信息的发布请求,然后将我的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

,这是我提出邮政请求的飞镖/颤音代码,并期望一个称为“ set-cookie”的cookie。

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

任何帮助将不胜感激!让我知道您是否需要更多详细信息/代码。

I am creating a Flutter Web app that requires login verification. The user makes a post request with authentication information and then my Flask app with send a cookie back to the client.

Here is the code for the Flask App

@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

Here is the Dart/Flutter code where I make the POST request and expect a cookie called 'Set-Cookie'.

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);
    }
  }

As you can see, I don't receive this cookie on my browser, but the request is successful and I get the JSON message!

Dev Tools

BUT, when I make this same request on Postman, I get the JSON response AND the cookie.

Any help would be greatly appreciated! Let me know if you need any more details/code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心小果奶 2025-01-29 09:43:45

感谢 kris ,我意识到我是从flutter(client)提出请求到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);
    }
  }

Thanks to Kris, I realized I was making the request from Flutter (Client) to an IP rather than the domain name localhost. Because setting a cookie is domain specific, I couldn't see the cookie set in the developer console.

Here is the updated code

static testMethod() async {
    try {
      dio.options.extra['withCredentials'] = true;
      var response = await dio.post('http://localhost:5000/test');
      print(response);
    } catch (e) {
      print(e);
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文