loadrunner发送json_如何参数化json文件中的值-loadrunner

在LoadRunner中进行REST API脚本编写时,遇到需求要使脚本每次迭代时替换JSON体内的唯一参数。问题在于,尽管将大型JSON文件作为额外文件存储,并在BodyFilePath中引用,但参数值未被替换。解决方案是,不能直接在加载的文件上做参数替换,需要先读取JSON文件,然后使用LR函数进行处理。示例代码包括lr_read_file和lr_eval_string等,用于手动加载JSON并将其作为请求体,实现了参数的动态替换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Need some help in loadrunner scripting with REST api. I have a requirement that LR script should always replace the unique parameter and this parameter should be part of Json body. File whatever i am using is huge. Hence, i created payload.json in extra files of LR. In Bodyfilepath i give this name of json file.

In the payload.json i have parameterised a value (which needs to be unique every iteration) for request to be succesful. However, this paramter value is not getting replaced.

Can anyone help me or share the code that helps to replace the value in the json file with the parameter value

Thank you

解决方案

It appears you cannot do parameter substitution directly on a loaded file and therefore you need to manually load the JSON and then use it as body.

Here is an example on how to do it:

lr_read_file("test.json", "test", 0);

lr_save_string(lr_eval_string(lr_eval_string("{test}")),"myjson");

lr_eval_json("Buffer={myjson}",

"JsonObject=myjson",

LAST);

lr_json_stringify("JsonObject=myjson","Format=compact","OutputParam=Result",LAST );

web_rest("My POST",

"URL=http://myserver.com",

"Method=POST",

"EncType=raw",

"Snapshot=t536990.inf",

// "Body={\"store\": \"{ts}\"}", this is what the JSON contains

// and I have a parameter named ts

"Body={Result}",

HEADERS,

"Name=Content-Type", "Value=application/json", ENDHEADER,

LAST);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值