Ashwin
Ashwin

Reputation: 479

In Jmeter, What would be syntax of parameters in Body Data section of HTTP Request Sampler, for Rest APIs and input should be generated dynamically

I'm trying to generate my input through JavaScript using BSF PreProcessor. Currently I've only BSF PrepProcessor and a HTTP Request. My Request body goes like this.,

{
    "entity": {
        "id": "${EnitityName}",
        ----
     }
}

The randomly generated string should be the value for ID, i've parameterized how we normaly do it. This is not working. Can somebody tell me where i'm going wrong or any alternative approach.

Upvotes: 1

Views: 1263

Answers (2)

Dmitri T
Dmitri T

Reputation: 168072

Given ${EnitityName} variable is properly set in your BSF PreProcessor script, it should be substituted by the generated value given BSF PreProcessor success.

I would suggest checking jmeter.log file for any errors - it should give you an idea regarding what went wrong. You can also share your BSF PreProcessor code so others and myself could take a look and suggest the fixes.

Actually according to JMeter Best Practices it is recommended to use JSR223 Elements so you should switch to JSR223 PreProcessor and Groovy language, the example code which generates a random alphanumeric string of 10 characters and putting the value into the ${EntityName} JMeter Variable will look like:

vars.put("EnitityName",org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric(10))

References:

Upvotes: 0

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

try using __RandomString function: (no need of JavaScript & BSF PreProcessor if you want a random string)

{
    "entity": {
        "id": "${__RandomString(8,abcdefghiz,)}",
        ----
     }
}

From Docs:

Examples:

${__RandomString(5)} will return a random string of 5 characters which can be readable or not

${__RandomString(10,abcdefg)} will return a random string of 10 characters picked from abcdefg set, like cdbgdbeebd or adbfeggfad, …

${__RandomString(6,a12zeczclk, MYVAR)} will return a random string of 6 characters picked from a12zeczclk set and store the result in MYVAR, MYVAR will contain string like 2z22ak or z11kce, …

Upvotes: 1

Related Questions