user2237424
user2237424

Reputation: 321

Variables in httprequest post body

I'm trying to generate a jmeter script where a unique folder is created each time the script is run - adding a variable of some sort to the folder name, such as a username+timestamp, should be enough to guarantee uniqueness. However, jmeter is not resolving the variable to its value - although it is when the variable is read out of a csv file (which isn't suitable).

Basically, I'm editing the PostBody in the http request, as follows:

 {"alf_destination":"workspace://SpacesStore/90368635-78a1-4dc5-be9e-33458f09c3f6","prop_cm_name":"Test
 Folder - ${variable}","prop_cm_title":"Test
 Folder","prop_cm_description":"Test Folder"}

where variable is basically any variable I've tried so far (such as a random string, timestamp, etc.)

Can anyone suggest how to get the variable resolved?

Upvotes: 27

Views: 91536

Answers (4)

Arkadeep Basu
Arkadeep Basu

Reputation: 31

suppose you have the value "NewYork" in jmeter variable "Location".

Use it like this in HTTP POST BODY DATA:

{location:"${Location}"} => which gets interpreted as {location:"NewYork"}

Upvotes: 3

z2z
z2z

Reputation: 529

You can use jmeter (since 2.9 version) uuid feature -> http://jmeter.apache.org/usermanual/functions.html#__UUID

${__UUID}

and

1) If you want just 1 value for the whole test, add a "User Defined Variables" Config Element to your test. This will be evaluated when you load the test script the first time.

2) If you want to have the value change for every thread execution, but stay the same during each thread instance: under your 'Thread Group', add a 'Pre Processors -> User Parameters' to your thread group - and add the variable there.

Also, if you want the value to change each time the thread starts over (each 'iteration' of the script within the thread group), you can check the "Update Once Per Iteration" box on the User Parameters - and it will get a new value each time it starts the thread over at the beginning of th test script (within that thread group).

http://mail-archives.apache.org/mod_mbox/jmeter-user/201208.mbox/%[email protected]%3E

Upvotes: 33

Risadinha
Risadinha

Reputation: 16666

With JMeter 2.9, the following works:

In HTTP Request Sampler, Tab "Post Body" add for example your JSON data and include the variables in it:

{"uuid":"${new-uuid}"}

new-uuid is a user defined variable. This will send (from View Results Tree, Tab "Request"/"Raw"):

POST data:
{"uuid":"a1b2c3d4e5f6"}

Upvotes: 24

user2237424
user2237424

Reputation: 321

I did this by referencing a variable in the http request post body - ${formvalues} - created using a beanshell preprocessor which is appended to the http request sampler.
Beanshell contents:

double random = Math.random();    
String formvalues ="{\"alf_destination\":\"workspace://SpacesStore/90368635-78a1-4dc5-be9e-33458f09c3f6\",\"prop_cm_name\":\"Test Folder - ${uname}_" + random + "\",\"prop_cm_title\":\"Test Folder\",\"prop_cm_description\":\"Test Folder\"}";         
vars.put("formvalues",formvalues);

So this creates a folder with the username (${uname}, taken from the csv) plus a random number - it's crude as there could potentially still be cases where the script tries to create a folder with the same name as an existing one, but it will work for my case.

Upvotes: 5

Related Questions