邮递员:如何删除请求前脚本中的cookie? [英] Postman: How do you delete cookies in the pre-request script?
问题描述
我见过的所有邮递员cookie管理答案都涉及浏览器扩展程序(打开chrome,删除cookie拦截器等)或通过应用程序使用UI手动管理cookie。
All the postman cookie-management answers I've seen refer to either the browser extension (open chrome, delete cookies viz interceptor etc) or with the app, using the UI to manually manage cookies.
我希望在请求前代码中删除某些cookie,以此作为编写API测试脚本的一部分。 (以编程方式删除它们)
I would like to delete certain cookies in my pre-request code as part of scripting my API tests. (delete them programmatically)
Sandobx API文档提到了 pm.cookies
,所以我尝试了
The Sandobx API docs mention pm.cookies
so I tried
if (pm.cookies !== null) {
console.log("cookies!");
console.log(pm.cookies);
}
但是 pm.cookies
数组为空。
还有 postman.getResponseCookies
,该参数为空(我想是因为我们在请求前部分,而不是测试部分)
There's also postman.getResponseCookies
, which is null (I assume because we're in the pre-request section, not in the test section)
一个答案建议致电postman-echo服务删除cookie。我尚未对此进行调查,但感觉不正确。
One answer suggested calling the postman-echo service to delete the cookie. I haven't investigated this yet, but it doesn't feel right.
推荐答案
自2019/08以来,新版本现已支持该功能,请在此处查看更多示例:以编程方式删除Cookie·问题#3312·postmanlabs / postman-应用程序支持
new version now supports that since 2019/08, see more examples here: Delete cookies programmatically · Issue #3312 · postmanlabs/postman-app-support
const jar = pm.cookies.jar();
jar.clear(pm.request.url, function (error) {
// error - <Error>
});
获取所有cookie
get all cookies
const jar = pm.cookies.jar();
jar.getAll('http://example.com', function (error, cookies) {
// error - <Error>
// cookies - <PostmanCookieList>
// PostmanCookieList: https://www.postmanlabs.com/postman-collection/CookieList.html
});
获取特定的Cookie
get specific cookie
const jar = pm.cookies.jar();
jar.get('http://example.com', 'token', function (error, value) {
// error - <Error>
// value - <String>
});
这篇关于邮递员:如何删除请求前脚本中的cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!