Postman:如何删除预请求脚本中的cookies? [英] Postman: How do you delete cookies in the pre-request script?

查看:21
本文介绍了Postman:如何删除预请求脚本中的cookies?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的所有邮递员 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.

作为编写 API 测试脚本的一部分,我想删除我的预请求代码中的某些 cookie.(以编程方式删除它们)

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 数组是空的.然而在控制台中,GET 调用会传递一个 cookie.

But the pm.cookies array is empty. Yet in the console, the GET call then passes a cookie.

还有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-app-support

new version now supports that since 2019/08, see more examples here: Delete cookies programmatically · Issue #3312 · postmanlabs/postman-app-support

要授予程序访问权限的 Cookie 域必须是 列入白名单.

Cookie domains to be given programatic access must be whitelisted.

const jar = pm.cookies.jar();

jar.clear(pm.request.url, function (error) {
  // error - <Error>
});

获取所有 cookie

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

const jar = pm.cookies.jar();

jar.get('http://example.com', 'token', function (error, value) {
  // error - <Error>
  // value - <String>
});

这篇关于Postman:如何删除预请求脚本中的cookies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆