Akhil Kumar Patnaik
Akhil Kumar Patnaik

Reputation: 45

Need to form custom request in jmeter

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.

Upvotes: 1

Views: 693

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

  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:

Upvotes: 2

Related Questions