Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMeter Alter HTTP Headers During Test

I'm attempting to test an HTTP service with JMeter. The HTTP service requires authentication through a simple bearer token mechanism. I'm trying to proceed as follows:

  1. POST authentication request
  2. Store token as a variable
  3. Set Authorization HTTP header to stored variable
  4. Request protected resource(s)
  5. Evaluate performance

So far I've been able to post the request, get the token, extract it with regex, save it to a variable, and assert that the variable is appropriately set.

The problem is getting the variable into the HTTP header. In the "Header Manager" the value is set like this:

Header Manager Attempt

Unfortunately when the next requests are issued their authorization header has the value "Bearer ". Searching around the internet led me to believe that headers are configured before the thread starts, which would explain the "Bearer "

My sampling/grouping/nesting is as follows:

JMeter Nesting

All the tests pass up to get restricted resource, which fails with a 400, since the authorization header is malformed.

I feel like I'm missing something really obvious, and/or approaching this problem the wrong way.

like image 842
Bennett Avatar asked Jul 02 '14 23:07

Bennett


People also ask

How do you parameterize a header in JMeter?

If you need to add 5 dynamic headers one by one, i.e. one header per request just change the value on each consecutive request - it's pretty easy doable, just place your CSV Data Set Config as a child of required request, the same for HTTP Header Manager.

What is the HTTP header manager used for in JMeter?

The http header manager in JMeter is the magic lasso that contains and maintains the HTTP headers that are sent to the server from the browser in use for scenerio recording. Each time the browser sends a request to a server, headers with additional information are attached to the request.

How do I pass basic authentication in JMeter?

Simple Ways to Implement Basic Authentication in JMeter We can use online tools to encode our string and paste it into the Header Manager. We should take care to add “basic” before our encoded credentials. If everything goes well, we should receive a 200 response code from the server.


1 Answers

You can dynamically construct your authorization header using Beanshell PreProcessor as follows:

  1. Add empty HTTP Header Manager as a child of your request which requires authorization

  2. Add Beanshell PreProcessor as a child of the same request with the following code:

    import org.apache.jmeter.protocol.http.control.Header;  sampler.getHeaderManager().add(new Header("Authorization","Bearer " + vars.get("BEARER"))); 

This will construct fully dynamic header using BEARER variable.

  • sampler is a shorthand to HTTPSamplerProxy class which gives access to parent Sampler instance
  • vars is the instance of JMeterVariables class which allows read/write access to all JMeter variables available within the bounds of current context (usually current Thread Group)

See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting and kind of Beanshell cookbook.

like image 199
Dmitri T Avatar answered Oct 02 '22 10:10

Dmitri T