1

I am in need to create a custom request in jmeter which looks like the below format:

{
"items": [
        {
            "id": "1",
            "productId": 1234
         }
        {
            "id": "2",
            "productId": 1218
        }
....
}

Here I have to generate some random number in between 10-15 and create the id blocks(based on the random number). Could someone please help how can I form the request accordingly and achieve this in jmeter.

Thanks in advance.

1 Answers1

2
  1. Add JSR223 PreProcessor as a child of the request which need to send this generated value

  2. Put the following code into "Script" area

    import groovy.json.JsonBuilder
    import org.apache.commons.lang3.RandomUtils
    
    
    def items = []
    def itemsNo = RandomUtils.nextInt(10, 16)
    
    1.upto(itemsNo) { id ->
        def productId = RandomUtils.nextInt(1111, 10000)
        def item = [:]
        item.put('id', id as String)
        item.put('productId', productId)
        items.add(item)
    }
    
    def payload = new JsonBuilder([items: items]).toPrettyString()
    vars.put('payload',payload)
    
  3. Use ${payload} JMeter Variable where you need to refer the generated JSON

Demo:

enter image description here

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Hi Dmitri, can we use the csv dataset config to pick the product id's from that file? If yes, then how to use it in your custom code? – Akhil Kumar Patnaik Feb 17 '21 at 18:23
  • tried to use the csv dataset variable name in your custom code. But it is picking the same product id value in the same request. modified code: 1.upto(itemsNo) { id -> def productID = ${prodid} def item = [:] item.put('id', id as String) item.put('productID', productID) items.add(item) } sample output: { "items": [ { "id": "1", "productID": 1919 }, { "id": "2", "productID": 1919 } ] } – Akhil Kumar Patnaik Feb 18 '21 at 07:35
  • Don't inline [JMeter Functions or Variables](https://jmeter.apache.org/usermanual/functions.html) into groovy scripts, only first occurrence will be cached and used, replace your `${prodid}` with `vars.get('prodid'}`. **vars** stand for [JMeterVariables](https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html) class instance, see [Top 8 JMeter Java Classes You Should Be Using with Groovy](https://www.blazemeter.com/blog/top-8-jmeter-java-classes-you-should-be-using-with-groovy) article for more details. – Dmitri T Feb 18 '21 at 07:59
  • I tried to use `vars.get('prodid')` but still the value is getting same in the request. `{ "items": [ { "id": "1", "productID": "1919" }, { "id": "2", "productID": "1919" } ] }` – Akhil Kumar Patnaik Feb 18 '21 at 10:11
  • In case of [CSV Data Set Config](https://jmeter.apache.org/usermanual/component_reference.html#CSV_Data_Set_Config) JMeter reads the next value for each user for each iteration, if you want to read all the values in one shot you need to read the file in Groovy, something like: `new File('browser.txt').readLines().get(id)`. More information: [The Groovy Templates Cheat Sheet for JMeter](https://www.blazemeter.com/blog/the-groovy-templates-cheat-sheet-for-jmeter) – Dmitri T Feb 18 '21 at 10:15