Ghost
Ghost

Reputation: 569

Jmeter Variable in POST Request body and Changing it before Request is sent sending

I have a Post HTTP request with body that has a variable in it's XML, this variable takes the hostName as a variable i.e: 123@host

 <Type>myType</Type>
  <FolderPath>Automation</FolderPath>
  <Description />
  <dest>123@host</dest>
  <Notes />

in JSR223 Pre-Processor groovy I like to change the host to ${host}. I know I have to read the body in a string then change it like this:

body = body.replaceAll("host", vars.get("host") );

How can I read the body in a String then change the HTTP request body then place it back in the HTTP Post body before request is sent?

Upvotes: 3

Views: 2699

Answers (2)

Dmitri T
Dmitri T

Reputation: 168072

The function you're looking for is HTTPSamplerBase.addNonEncodedArgument()

Example Groovy code to replace host with the relevant JMeter Variable would be something like:

def body = sampler.getArguments().getArgument(0).getValue()
body = body.replaceAll('host',vars.get('host'))
sampler.getArguments().removeAllArguments()
sampler.addNonEncodedArgument('',body,'')
sampler.setPostBodyRaw(true)

Upvotes: 2

Kristijan Rusu
Kristijan Rusu

Reputation: 577

You are on the right track, by using the PreProcessor. Next you should put back that string in the same or new variable.

You can put back that body to the same variable like this:

vars.put("host",body);

Or you can change the sampler by calling:

sampler."method"

You can find all available methods on this link: Sampler Methods

Hope this helps.

Upvotes: 0

Related Questions