Search code examples
jmeterbeanshell

How to modify HTTP request before sending in JMeter through Beanshell pre processor?


I have test case in my csv file. The request URL has a custom variable.

Sample URL : .../abc/$id

I need to replace this id by the id that we get in response from the previous request. I used json extractor to fetch the id from the response. Now I need to update this id in the next test case request. Fetched the Request URL from jmeter context using below code:

String path = ctx.getCurrentSampler().toString(); 
path.replaceAll("$id", id);

I am not able to set this updated URL in jmeter context (ctx)


Solution

    1. You need to assign new path value to path variable
    2. You need to set sampler path to the new value using sampler.setPath() method

    So you need to amend your code like:

    String path = ctx.getCurrentSampler().toString();
    path = path.replaceAll("$id", id);
    sampler.setPath(path);
    

    Demo:

    JSR223 PreProcessor change path


    Also consider switching to JSR223 PreProcessor and Groovy language as Groovy performance is much higher, it better supports new Java features and provides some extra "syntax sugar" on top. See Groovy is the New Black article for details.