Skip to main content

How to Extract Auth-Token, Session Id, and Other Header Parameters in JMeter?

Table of Contents:

  • Introduction
  • What is the Regular Expression Extractor
  • How to use the JMeter Regular Expression Extractor

Introduction

The performance test engineer often encounters situations while working with performance testing with JMeter, when it is necessary to extract auth – token, session Ids, or other response header parameters when we request an API. The Regular Expression Extractor comes into the picture, and it comes under Post Processors. Using this Regular Expression Extractor, we can extract the values and store them into the variable and use the same variable later into the subsequent request. The Regular Expression Extractor works as a tool to extract a required part of the text by using advanced manipulations.

What is Regular Expression Extractor?

In performance testing with JMeter, the Regular Expression Extractor is useful for extracting information from the response. For Example, when we request anything in response, we will get response data to extract those data or saving the extracted information to a variable, so it can be used later in the performance test, for example when testing an application that uses token authentication.

How to use the Regular Expression Extractor?

In this article we are going to see how to use the Regular Expression Extractor in JMeter:

Locating the Token

Let’s use the login API action of any application and observe the traffic communication between the client and the server. We can see there is a token given into the header of the response. As we can see, x-auth-token in below screenshot.

Extracting the Token with the JMeter Regular Expression Extractor

Now we need to extract the token from the login API call. To do this we have to add a JMeter regular expression extractor to the login request right click on the request.

And set configuration as it follows:

  • Apply to: We can choose the whether the regular expression will be applied to the main sample results, to the sub-samples, or both here we applied to Main sample only
  • Filed to check: Response Header, because we want to extract a value that is given in the header of the response.
  • Name of created variable: Token1 (we can choose the name whichever we want)
  • Regular Expression: fill in the regular expression to test
  • Template: $0$ to indicate will extract the entire expression
  • Match No: Number of matches of the expression in the text we want to save
  • Default Value: Enter the text we want in the variable if the expression does not match. Here added Token not found if the expression does not match

Now we can use this regular expression variable wherever needed in the subsequent API’s, i.e., extracted value of auth-token.

Problem statement for one of Live project –

In this project we have issue for 401 error for load test scripts because all the API calls need a unique auth token for each subsequent call in the load test scripts.

To overcome this problem, we have applied the below configuration solution.

  1. Use the Regular Expression Extractor to login request to extract the auth-token from response header using (How to use the Regular Expression Extractor)
  2. And use this Regular Expression Extractor variable for each subsequent call to get auth token

We at Varseno, can run customized performance testing using Apache JMeter for all business websites to improve their speed, scalability and stability. Reach out to us to improve your website functionalities in terms of performance.

Sales Inquiry
close slider