Adjust HTTP request before sending in JMeter

By | May 3, 2015

In JMeter, you can make HTTP requests with different settings such as using different verbs (GET, POST, etc) and sending data (through query string, request body). When it is time to adjust request in a slightly custom way before sending, it might be complicated in the first attempt. With BeanShell PreProcessor, it turns to be very easy.

In my case, I need to adjust request URI path with user variables so that it maps to correct request address. Sure, there might be some other ways to accomplish the same goal, but after experiencing how flexible it is to adjust request with BeanShell PreProcessor, I think it can satisfy lots of different cases at varied complex levels.

In my case, I need to make such a request:

GET http://localhost/Client/1/Order?Token=123456

In this request, the number coming after Client, 1, presents the client id. The value of parameter named Token presents the token value. Client id and token value are user variables in my JMeter project, which might come from preconfigured sources. Indeed, for simplicity, I added them directly just shown as below:

User variables

Here is how the request is set up:

Request set up

As you can see from Path field, it is set as only Order, so without any additional configuration, the request would be like this:

GET http://localhost/Order

Sure, this is not expected result so I add a BeanShell PreProcessor under the request, which is the one named “Adjust path”.

Adjust_path

In the preprocessor, I place 4 lines to adjust request URI. Here is what I do line by line:

var sampler = ctx.getCurrentSampler();

Here, I access the current sampler object, which would be HTTPSampler in this case. You can find all details (methods, properties) about this sampler through the following link:

Click here to see HTTPSampler

var path = sampler.getPath();

After getting HTTP sampler object, I retrieve the path value, then create a custom path value using user variables.

var customPath = "Client/" + vars.get("ClientId") + "/" + path + "?Token=" + vars.get("AccessToken");

vars.get(“Variable_name”) is the way to get the value of given variable by name.

Lastly, I set the new path as below:

sampler.setPath(customPath);

With these lines, I can make the HTTP request as I want to be.

A few notes regarding the implemantation:

I am sure that it is not one of best samples to demonstrate extensibility and power of BeanShell PreProcessor in adjusting request and there might be more simple ways to do it to some extent. For example, if you set up the path field directly as below, you end up with the same result:

Client/${ClientId}/Order?Token=${AccessToken}

I find using BeanShell PreProcessor more clean as you embed the path construction logic and leave paths as simple as possible. Nevertheless, if you need to adjust request before sending, keep it in mind that you have a good friend, BeanShell PreProcessor, which offers pretty flexible options.

One thought on “Adjust HTTP request before sending in JMeter

  1. Dmitri T

    Thanks for the great tutorial. The only thing I would amend is using JSR223 PreProcessor instead of Beanshell.

    The reason is that Beanshell interpreter has some performance issues and in case of severe load it may become a bottleneck of your test. So it is recommended to use JSR223 Test Elements and Groovy language as it implements Compilable interface and hence Groovy scripts (assuming good configuration) can be almost as fast as pure Java code. Valid Java code will be in 99% valid Groovy code so you won’t have to change a single line.

    By the way, it is better to use StringBuilder for strings concatenation.

    See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You’ve Been Waiting For! for different scripting approaches performance benchmark, instructions on how to setup Groovy engine in JMeter and scripting tips and tricks.

    Reply

Leave a Reply

Your email address will not be published.