通过增加jmeter中变量的值来创建动态请求

时间:2018-08-30 13:39:07

标签: json jmeter

I have a response from one request as :

**{  
   "Packages":[  
      {  
         "CreatedBy":"Administrator",
         "CreatedDate":"\/Date(1535635263383)\/",
         "DeviceFamily":6,
         "LastVersion":{  
            "BuildVersion":"1.0",
            "Comment":"",
            "ExtraSize":0,
            "InstallPrompt":false,
            "OSVersion":"2.0-10.0",
            "Processor":"ALL",
            "Size":10465294,
            "UploadDate":"\/Date(1535635263520)\/",
            "UploadedBy":"Administrator",
            "Vendor":"",
            "Version":"1.0"
         },
         "Name":"10MB",
         "ReferenceId":"150e6517-f805-4e33-936f-d923657be79a",
         "TotalVersions":1
      },
      {  
         "CreatedBy":"Administrator",
         "CreatedDate":"\/Date(1535635276557)\/",
         "DeviceFamily":6,
         "LastVersion":{  
            "BuildVersion":"1.0",
            "Comment":"",
            "ExtraSize":0,
            "InstallPrompt":false,
            "OSVersion":"2.0-10.0",
            "Processor":"ALL",
            "Size":104884390,
            "UploadDate":"\/Date(1535635277457)\/",
            "UploadedBy":"Administrator",
            "Vendor":"",
            "Version":"1.0"
         },
         "Name":"100MB",
         "ReferenceId":"02e94794-2e0a-4c20-9d20-5612a9b2378f",
         "TotalVersions":1
      }
   ],
   "Total":2
}**

上面的json包含两个ReferenceId的值:Name,Version和Size。 这些值需要传递到后续请求中,并创建动态请求,以便在上述变量的值增加到4,6或N时,请求可以修改。

I have to pass values in next request as :

**{  
   "HasDraftVersion":false,
   "IsApprovalPending":null,
   "IsInstalledSilently":true,
   "LastModified":"/Date(1535570626907)/",
   "LastModifiedBy":"Administrator",
   "DeviceConfigurations":[  
   ],
   "Packages":[  
      {  
         "ReferenceId":"b0f3b725-86f9-4bc3-a929-63bfb8e8727c",
         "Name":"10MB",
         "Version":"1.0",
         "PackageDependencies":"",
         "Size":10465294
      },
      {  
         "ReferenceId":"bb6b3176-0798-4cef-9659-4a119470a4dc",
         "Name":"100MB",
         "Version":"1.0",
         "PackageDependencies":"",
         "Size":104884390
      }
   ],
   "LdapTargets":null,
   "ReferenceId":null,
   "DeviceFamily":6,
   "DeviceFamilyQualification":0,
   "Name":"test",
   "Description":"",
   "Random":"2018-08-29T15:16:47",
   "IsPublicAPI":true
}**

**如何在前一个json中创建具有增加值的动态请求?

我正在尝试使用Json路径提取器,但是无法创建动态请求。

我也在尝试使用正则表达式提取器。**

1 个答案:

答案 0 :(得分:0)

  1. JSR223 PreProcessor添加为第二个请求的子项
  2. 将以下代码放入“脚本”区域:

    def previousResponse = new groovy.json.JsonSlurper().parse(ctx.getPreviousResult().getResponseData())
    
    @groovy.transform.Immutable
    class Package {
        String ReferenceId
        String Name
        String Version
        Integer Size
    }
    
    def packages = previousResponse.Packages.collect { Package -> new Package(Package.ReferenceId, Package.Name, Package.LastVersion.Version, Package.LastVersion.Size) }
    
    def builder = new groovy.json.JsonBuilder()
    
    builder(HasDraftVersion: false,
            IsApprovalPending: null,
            IsInstalledSilently: true,
            LastModified: "/Date(1535570626907)/",
            LastModifiedBy: "Administrator",
            DeviceConfigurations: [],
            Packages: packages.collect() {
                [
                        ReferenceId        : it.ReferenceId,
                        Name               : it.Name,
                        Version            : it.Version,
                        PackageDependencies: "",
                        Size               : it.Size
                ]
            },
            LdapTargets: null,
            ReferenceId: null,
            DeviceFamily: 6,
            DeviceFamilyQualification: 0,
            Name: "test",
            Description: "",
            Random: "2018-08-29T15:16:47",
            IsPublicAPI: true)
    
    sampler.getArguments().removeAllArguments()
    sampler.addNonEncodedArgument('',builder.toPrettyString(),'')
    sampler.setPostBodyRaw(true);        
    

以上代码将自动从上一个响应中提取所需的值,使用这些值动态构建请求并将其添加到HTTP Request采样器主体中。

更多信息: