Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform HTTP-Request during pre-processing in Jmeter?

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?

like image 761
Akash Mehta Avatar asked Dec 31 '25 13:12

Akash Mehta


1 Answers

There is JSR223 PreProcessor you can use to make an arbitrary HTTP Request using underlying Apache HttpComponent libraries.

  1. Add JSR223 PreProcessor as a child of your SSH Command sampler
  2. 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)
    
  3. 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:

JMeter Groovy Execute HTTP Request

References:

  • HttpClient Quick Start
  • Apache Groovy - Why and How You Should Use It
like image 114
Dmitri T Avatar answered Jan 05 '26 07:01

Dmitri T



Donate For Us

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