1
votes

I am creating a script on groovy to be able to send http requests.
To call this script I'm using the JSR223 Sampler
The thing is that I would like to reproduce as much as possible the behaviour that a HTTP Sampler has, meaning that I want to perform the request and also to fill the sampler's info (Response data, Request and Response)
Although I'm able to obtain SampleResult and set Response data and Response, does not seem to exist a method to set our own request string:
https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html

Following the docs, the closest method to do what I want is the setRequestHeaders().

If I call that method like this SampleResult.setRequestHeaders("My custom text") something like this appears on the request tab:

File C:\Users\UserName\groovy_file.groovy
Request Headers:
My custom text

Is there any way to print only the string My custom text, on the Request?

EDIT

Sampler must use a script file instead of the script field

1

1 Answers

3
votes

The easiest way would be just overwriting the data using prev.samplerData() shorthand from the JSR223 PostProcessor

prev.samplerData = 'put the desired request data here'

where prev stands for the parent SampleResult class instance, check out Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on the JMeter API shorthands available for the JSR223 Test Elements.

If you don't want the PostProcessor you can still call the same function from your Groovy script like:

SamplerResult.setSamplerData('put the desired request data here')