使用静态指导的邮递员脚本

发布于 2025-01-28 10:52:48 字数 946 浏览 1 评论 0原文

我希望使用Postman生成和插入我正在从事的API项目的测试数据。 API中的资源具有外键约束,因此我希望能够为资源生成静态guid s,并在所有其他请求中共享这些ID,以便我们可以维持参考文献并拥有有效的外国钥匙。

目前,我正在生成我想在集合级别的请求脚本中以收藏夹中所有请求保持的数据:

// set the user ID for our test user
var userId = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("userId", userId);

// then generate a product ID
var productId = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("productId", productId);

// create a few vendors
var vendorId1 = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("vendorId1", vendorId1);

var vendorId2 = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("vendorId1", vendorId1);

// etc

我的想法是我在所有后续发布 s,因此将有一个真正的用户可以将新对象关联,然后将供应商 ids与产品插入等等。

但是,请求脚本在每个请求之前运行 - 我认为它正在重新创建所有这些值,而不是保持它们的静态,因此我保持完整性的计划不起作用。

有更好的策略吗?例如,我可以从请求响应中捕获插入对象的ID并将该变量传递给工作流中的下一个请求,以便它可以将ID用于关联资源?

I'm hoping to use Postman to generate and insert test data for an API project I'm working on. The resources in the API have foreign key constraints, so I want to be able to generate static guids for the resources, and share those IDs among all the other requests so that we can maintain the references and have valid foreign keys.

At the moment I'm generating data that I want to keep accessible to all the requests in the collection in a pre-request script at the collection level:

// set the user ID for our test user
var userId = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("userId", userId);

// then generate a product ID
var productId = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("productId", productId);

// create a few vendors
var vendorId1 = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("vendorId1", vendorId1);

var vendorId2 = pm.variables.replaceIn("{{$guid}}"); 
pm.collectionVariables.set("vendorId1", vendorId1);

// etc

My thought was that I'd use the userId in all subsequent POSTs so there'd be a real user to associate a new object with, then use the vendor IDs with the product inserts, etc.

However, the pre-request script runs before each request-- I think it's re-creating all these values rather than keeping them static, so my plan to maintain integrity isn't working.

Is there a better strategy? For example, can I capture the ID of the inserted object from the request response and pass that variable on to the next request in the workflow so that it can use that ID for the associated resources?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2025-02-04 10:52:48

您可以测试是否已经设置了用户ID,并且只有当它未设置您的变量时,例如:

if (!pm.collectionVariables.get("userId")) {
    // set the user ID for our test user
    var userId = pm.variables.replaceIn("{{$guid}}"); 
    pm.collectionVariables.set("userId", userId);

    // set your other variables...
}

You could test if the userId is already set, and only if it isn't set your variables, something like:

if (!pm.collectionVariables.get("userId")) {
    // set the user ID for our test user
    var userId = pm.variables.replaceIn("{{$guid}}"); 
    pm.collectionVariables.set("userId", userId);

    // set your other variables...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文