
一、Apifox登录接口后置操作中添加脚本提取token方式1 // 获取响应头中的 Set-Cookie 值 const cookieHeader pm.response.headers.get(Set-Cookie); if (cookieHeader) { // 解析 cookie 中的 token 值 const match cookieHeader.match(/csccs_auth_token([^;])/); if (match) { const token match[1]; pm.environment.set(authToken, token); console.log(Token 提取成功:, token); } } 方式2 // 获取响应头中的 Set-Cookie const setCookieHeader pm.response.headers.get(Set-Cookie); if (setCookieHeader) { // 提取 csccs_auth_token 的值只匹配到第一个 ; 之前 const match setCookieHeader.match(/csccs_auth_token([^;])/); if (match) { const token match[1]; // 保存为环境变量 pm.environment.set(authToken, token); console.log(Token 提取成功长度:, token.length); console.log(Token 前 50 个字符:, token.substring(0, 50) ...); } else { console.warn(未找到 csccs_auth_token); } } else { console.warn(响应头中没有 Set-Cookie); }二、后续用到token的接口headers里面增加参数三、自动化测试