JSR223 Post Processor for Extracting URL

36 Views Asked by At

I was using this groovy script to take a dynamic value to correlate from a URL.

// Retrieve the URL as a string from the previous sampler response
def urlString = prev.getUrlAsString()
 
// Define the regular expression pattern to capture pzPostData parameter
def regexPattern = /pzPostData=(-?\d+)/
 
// Match the pattern against the URL string
def matcher = urlString =~ regexPattern
 
// Check if a match is found
if (matcher.find()) {
    // Extract and store the captured group (section)
    def PostData = matcher.group(1)
    log.info("Extracted PostData: $PostData")
} else {
    log.info("No match found for the pattern in URL: $urlString")
}

The url goes along the line of "www.random.com/web/STANDARD?!PostData=123456"

I only want the value after PostData.

The log shows that it is picked up;however, when trying to put it in one of my requests as something like ${PostData} it does not get picked up and added to another URL request.

Not sure what to do and what is wrong. If this info helps, the request has two sub-requests when making that one request. Its because it is a 303 and redirects itself to the new page with the URL im trying to pick up to add to another request.

Let me know if theres a better way to do this

1

There are 1 best solutions below

0
Ivan G On

If there is a redirect you need to extract the URL from a sub-result, not from the main sampler.

Something like "Apply to" field of the Regular Expression Extractor:

enter image description here

So I think you need to change this line:

def urlString = prev.getUrlAsString()

to this one:

def urlString = prev.getSubResults().find { subResult -> subResult.getUrlAsString().contains("PostData") }.getUrlAsString()

More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?