在 Postman 中访问集合变量 [英] Accessing Collection Variables in Postman

查看:54
本文介绍了在 Postman 中访问集合变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能

Postman 添加了对集合的变量、授权、预请求和测试脚本的支持.(从 5.4.1 版开始,这在集合和文件夹级别都存在)

Postman added support for variables, authorization, pre-request and test scripts to collections. (as of version 5.4.1 this exists at both the collection AND folder level)

用例

假设我想在登录端点被点击时存储一个刷新令牌.我的测试脚本需要创建/更新 COLLECTION 变量,而不是全局或环境变量.

Let's say I want to store a refresh token when the login endpoint is hit. My test script needs to create/update a COLLECTION variable, NOT a global or environment variable.

一旦 refresh_token 可用于集合、其他测试和预请求脚本,我认为有一种方法可以通过类似于 pm.environmentpm 的 API 访问它们.globals.(例如pm.collection)

Once that refresh_token is available to the collection, other tests and pre-request scripts, I would think there is a way to access them through an API similar to pm.environment or pm.globals. (pm.collection, for instance)

问题

我找不到有关如何通过预请求脚本或测试访问或修改这些内容的任何文档...有谁知道如何执行此操作?也许这还没有完全考虑,或者没有完全实施,但我想我会向其他人寻求帮助.

I cannot find any documentation on how to access or modify those via pre-request scripts, or tests... does anyone know how to do this? Maybe this hasn't been thought out completely, or not fully implemented, but I thought I would check with others for some help.

临时解决方案

作为一个完整的黑客,我将我需要的东西存储为命名空间环境变量.这并不理想(在其他集合中工作时会使事情变得有点混乱)但它工作得很好.

As a complete hack, I am storing the things I need as namespaced environment variables. It's not ideal (makes things kind of messy when working in other collections) but it works just fine.

推荐答案

Postman v7.9.0 添加了对新 pm.collectionVariables 的支持,因此您可以在测试脚本上更新它们:

Postman v7.9.0 added support for new pm.collectionVariables, so you can update them on test scripts:

pm.collectionVariables.set("collection_variable", newValue);

https://github.com/postmanlabs/postman-app-support/issues/5053#issuecomment-542555156

这篇关于在 Postman 中访问集合变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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