如何解析响应 cookie 并将特定值发送回请求 header ?
我正在发出请求:它会在 session cookie 中发回 token (token=longstrong
)。我需要获取该 cookie,解析出 token
,并在 x-token:
请求 header 中发回以下请求的值。
Paw 只给我发送 cookie(原始)的选项。
如何解析响应 cookie 以发回 $.token
的值(json 伪代码)?
最佳答案
回复晚了,抱歉!
这可能会有所帮助(来自 How do i pick specific cookies? ):
改用自定义动态值(右键单击该字段,然后选择扩展> 自定义),并使用以下 JavaScript代码片段:
function evaluate(context){
// Set here the cookies you'd like to return
var wantedCookies = ["datr", "reg_fb_ref"];
var regex = /^(\w+)\=([^;\s]+)/g;
// Request
// Uses here the current request, you can use getRequestByName("name of the request") instead
var request = context.getCurrentRequest();
// Get response cookies
var cookies = request.getLastExchange().getResponseHeaderByName("Set-Cookie").split(", ");
var filteredCookies = [];
for (var i in cookies) {
var cookie = cookies[i];
var match = regex.exec(cookie);
if (match && wantedCookies.indexOf(match[1]) >= 0) {
filteredCookies.push(match[0]);
}
}
return filteredCookies.join(",");
};
这基本上会手动解析响应 cookie,并返回您需要的内容。
另一个问题可能会有所帮助:Routes using cookie authentication from previous version of Paw no longer work on new version
关于cookies - macOS Paw - 解析请求 header 的响应 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54676504/