Cookie 和会话指南

欢迎使用 Finch Cookie 和会话指南!本指南将引导您完成在 Finch 应用程序中使用 cookie 和会话的步骤。无论您是经验丰富的开发人员还是刚刚起步,Finch 都提供了一套强大的工具来简化服务器端 Web 应用程序开发。

您可以使用 rq.getCookierq.addCookie 方法在 Finch 应用程序中读取或写入 cookie。以下是使用示例:

app.get(
  path: '/cookie',
  index: (rq) async {
    var value = rq.getCookie('test', def: 'default value');
    rq.addCookie('test', 'new value');
    return rq.renderString(text: 'Cookie value: $value');
  },
);

您可以使用 rq.addCookie 方法加密您的 cookie。以下是使用示例:

app.get(
  path: '/cookie',
  index: (rq) async {
    var value = rq.getCookie('test', def: 'default value', safe: true);
    rq.addCookie('test', 'new value', safe: true);
    return rq.renderString(text: 'Cookie value: $value');
  },
);

读取或写入会话

您可以使用 rq.getSessionrq.addSession 方法在 Finch 应用程序中读取或写入会话。以下是使用示例:

app.get(
  path: '/session',
  index: (rq) async {
    var value = rq.getSession('test', def: 'default value');
    rq.addSession('test', 'new value');
    return rq.renderString(text: 'Session value: $value');
  },
);