简体   繁体   中英

Jmeter - Passing specific JSON response to HTTP request dynamically

I am having a specific requirement in Jmeter(2.13) where i need to pass two parameters multiple times dynamically as id and parentObjectApiName

{
            "id":"SomeNumber",
            "parentObjectApiName":"SomeName"
         },
         {
            "id":"SomeNumber",
            "parentObjectApiName":"SomeName"
         },

}

Which i will be getting from a response as :

{
    "detailMap": {
      "RootNumber": [
        {
          "id": "SomeNumber",
          "properties": {

          },
          "isDeleted": false,
          "version": "2017-11-20T08:13:30+00:00",
          "referenceId": null,
          "parentObjectApiName": "SomeName"
        },
        {
          "id": "SomeNumber",
          "properties": {

          },
          "isDeleted": false,
          "version": "2017-04-21T15:40:10.742+00:00",
          "referenceId": null,
          "parentObjectApiName": "SomeName"
        },
        {
        :
        },
      ]
    }
    "state": {
      "errorDetails": []
    }
  }

Is there any workaround for the above requirement by using beanshell in Jmeter(2.13).

Your requirement can be achieved by following the steps below.

  1. Add "JSON Extractor" to the request where the response contains the parameters which you want to pass in next request.(Image-1)
  2. JSON Extractor configuration(Image-2)

Keeping the JSON Extractor as it is, Add "Beanshell PostProcessor" to the request and keep the following part of code and try. Your desired id & parentObjectApiName will be stored in variable "json". You can call it in your next request as ${json}

import java.io.file;
import java.util.*;
import org.apache.jmeter.services.fileserver;

StringBuilder output = new StringBuilder();
Random random = new Random();
int max = Integer.parseInt(vars.get("id_matchNr"));
for(int i=1;i<=max;i++)
{
output.append("{");
output.append("\"id\":\"" + vars.get("id_"+i) + "\",\"parentObjectApiName\":" + vars.get("parentObjectApiName_"+i));
output.append("}").append( "," );
}
String withoutLastComma = output.substring( 0, output.length( ) - ",".length( ) );
vars.put("json", withoutLastComma.toString());

Image-1

Image-2

Be aware that since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any scripting in JMeter. Groovy has much better performance than Beanshell does , moreover Groovy has built-in JSON support .

  1. Add JSR223 PostProcessor as a child of the request which returns the above JSON
  2. Make sure you have groovy selected in the "Language" dropdown and Cache compiled script if available box ticked
  3. Put the following code into "Script" area:

     import groovy.json.JsonBuilder import groovy.json.JsonOutput import groovy.json.JsonSlurper import groovy.json.internal.LazyMap def text = prev.getResponseDataAsString() log.info('Original response: ' + text) def json = new JsonSlurper().parseText(text) def data = new ArrayList() json.detailMap.RootNumber.each { rootNumber -> def map = new LazyMap() map.put("id", rootNumber.id) map.put("parentObjectApiName", rootNumber.parentObjectApiName) data.add(map) } vars.put('json',JsonOutput.prettyPrint(JsonOutput.toJson(data))) log.info('Generated json: ' + vars.get('json')) 

The above code will generate the following JSON:

[
    {
        "id": "SomeNumber",
        "parentObjectApiName": "SomeName"
    },
    {
        "id": "SomeOtherNumber",
        "parentObjectApiName": "SomeOtherName"
    }
]

JMeter Groovy生成JSON

You will be able to access it as ${json} where required (ie in next HTTP Request sampler "Body Data" tab)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM