Before the actual test execution, I want to call some HTTP APIs and parse the response out of it before handling it to my SSH Command sampler. What's the best way to do it in Jmeter? Like there is a pre-processor for JDBC request, then why not a pre-processor for HTTP request?
There is JSR223 PreProcessor you can use to make an arbitrary HTTP Request using underlying Apache HttpComponent libraries.
Put the following code into "Script" area:
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils
def httpClient = HttpClientBuilder.create().build()
def httpGet = new HttpGet("http://example.com")
def httpResponse = httpClient.execute(httpGet)
def responseData = EntityUtils.toString(httpResponse.getEntity())
log.info('----- Response -----')
log.info(responseData)
vars.put('response', responseData)
The above code executes simple HTTP GET request to http://example.com website, prints the response to jmeter.log and additionally stores it into ${response} JMeter Variable which you can reuse later on where required.
Demo:

References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With