模板字符串2
如果想获取响应阶段的状态码、服务端IP、响应头、响应cookie,可以通过以下两种方式设置规则:
- resScript
- 插件的resRulesServer
通过这两种方式设置的响应规则,除了可以设置上述请求信息,还可以设置如下响应信息:
pattern3 protocol://{test2.json}
test2.json:
{
"url": "${url}",
"search": "${url.search}",
"query": "${url.query}",
"queryValue": "${url.query.name}",
"host": "${url.host}",
"hostname": "${url.hostname}",
"path": "${url.path}",
"pathname": "${url.pathname}",
"reqId": "${reqId}",
"now": ${now},
"method": "${method}",
"xff": "${reqHeaders.x-forwarded-for}",
"other": "${reqHeaders.other}",
"cookie": "${reqCookie}",
"cookieValue": "${reqCookie.cookieName}",
"clientIp": "${clientIp}",
"statusCode": "${statusCode}",
"serverIp": "${serverIp}",
"resHeaderValue": "${resHeaders.x-res-header-name}",
"resCookieValue": "${resCookie.res_cookie_name}"
}
${xxx}
里面如果对应的值不存在则返回空字符串;如果涉及到 query、cookie 会自动 decode
,如果你不想自动对 key
和 value
做 decode
,可以加多一个 $${xxx}
。
v1.12.13
版本开始支持 replace(pattern,replacement)
功能,如:
protocol://`${search.replace(/course=([^&]+)/ig,name=$1)}`
protocol://`${search.replace(a,b)}`
学员评价