performancetestingloadrunnervugen

how to parameterize the value in json file - loadrunner


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

web_custom_request(
"web_custom_request", 
"URL=name of the URL/Service ", 
"Method=POST", 
"TargetFrame=", 
"Resource=0", 
"Referer=", 
"BodyFilePath=payload.json", 
LAST); 

Payload.json file is passed under Extra Files and sample looks like this:

{ "Msgheader":
{ "Field1":"AB", "Field2":"201300{test}", "Field3":"50.00", "CrBy":"", "CrOn":"2018-03-16", }


Solution

  • 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);