在邮递员中重复使用{{$ randomInt}} [英] Reuse {{$randomInt}} in Postman

查看:194
本文介绍了在邮递员中重复使用{{$ randomInt}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个请求是: GET http://example.com?int= {{{$ randomInt}}
我需要将第二个请求(以及其他测试)运行到相同的地址,因此我需要保存生成的变量。我该怎么办?

My 1st request is: GET http://example.com?int={{$randomInt}}. I need to run 2nd request (with other tests in it) to the same address, so I need to save generated variable. How can I do it?

我在尝试 $$$$ pm.variables.get( int)在第一个请求后测试沙盒,但此代码无法看到 int 变量。

I was trying pm.variables.get("int") in the "Tests" sandbox after 1st request, but this code cannot see int var.

在先决条件中创建随机数。沙箱到第一个请求:
postman.setGlobalVariable('int',Math.floor(Math.random()* 1000));
不会也没有帮助,因为我需要在URL中使用此参数,而需要 Pre-req。

Creating random number in Pre-req. sandbox to 1st request: postman.setGlobalVariable('int', Math.floor(Math.random() * 1000)); doesn't help either, because I need to use this param in the URL, while "Pre-req." block is run after request but before tests.

那么如何在第一个请求之前生成随机var并将其存储在第二个请求中使用?

So how can I generate random var before 1st request and store it to use in 2nd request?

推荐答案

如果在第一个请求的预请求脚本中进行了设置:

If you set this in the Pre-Request Script of the first request:

pm.globals.set('int', Math.floor(Math.random() * 1000))

Or

// Using the built-in Lodash module
pm.globals.set("int", _.random(0, 1000))

您将能够引用它并使用 {{int}} 语法在任何要求。如果您将其添加到第一个请求中,然后在URL http://first-example.com?int= {{{int}} 中使用它,则该值将保持不变并且您可以在第二个请求中再次使用它 http://second-example.com?int= {{int}}

You will be able to reference it and use the {{int}} syntax in any request. If you add this in the first request and then use it in the URL http://first-example.com?int={{int}} this value will then persist and you can use it again in a second request http://second-example.com?int={{int}}

每次使用 {{$ randomInt}} 时,它将在运行时生成新值。

Each time that {{$randomInt}} is used, it will generate a new value at run time.

这篇关于在邮递员中重复使用{{$ randomInt}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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