DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • A Beginner's Guide to Spark UI: Concepts and How to Use It
  • Multi-Tenant .NET Applications With Keycloak Realms
  • Observability and DevTool Platforms for AI Agents

Trending

  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  • A Deep Dive Into Firmware Over the Air for IoT Devices

Using Regular Expressions to Extract Tokens and Session IDs to Variables

This tutorial explains how to use regular expressions to extract tokens and session IDs to variables.

By 
Leticia Almeida user avatar
Leticia Almeida
·
Aug. 02, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
14.4K Views

Join the DZone community and get the full member experience.

Join For Free

Tokens and session IDs are sent in the request header. Sometimes, when scripting in Apache JMeter™, we need to work with them as variables. For example, in cases where the following requests use these session IDs or tokens in their parameters (in POST requests for example). So, we need to find a way to do this easily in JMeter. This post will review how to do this with the Regular Expression Extractor.

Let's get started.

Let's use as an example login action of the BlazeDemo website (Make sure you register and also check the "Remember Me" option to log in successfully).

If we look into the traffic communication between the client (our browser) and the endpoint or server (Blazedemo.com), we can see there is a token given into the header of the response, as a cookie field.

If you are wondering what I'm using to see this information, well, I'm using Fiddler, but you can also see this using Charles or the Network tab of the Developer Tool of your browser.

As you can see, this is an XSRF token.

So now, let's create a JMeter script for the Login step. To do this, you can use the BlazeMeter recorder (a free Chrome plugin).

Let's suppose that your script looks like this:

In Step 1, we go to the main login page, and in Step 2, we log in to the page with a user and password.

If we run this script, we can see the same response that we have shown before, but in this case, from JMeter itself. Let's look at the View Results Tree element. You can see the same XSRF token, with the same value:

So, what can we do if we need to set up a variable with the value of the XSRF-TOKEN? Here is where the Regular Expression Extractor comes to play.

We need to extract the token from the answer. To do this, we have to add a regular expression extractor to the request (right click over the request):

And set it as follows:

Selecting the options:

  • Field to check: Response Header, because we want to extract a value that is given in the header of the response.
  • Name of created variable: XSRF-TOKEN (you can choose the name you want).
  • Regular Expression: Set-Cookie: XSRF-TOKEN=(.*?);
  • Template: $1$ to indicate the group of the regular expression you want to save. In this case, we have only one group.
  • Match No: 1. Number of match of the expression in the text we want to save.
  • Default Value: NOT FOUND. Enter the test you want in the variable if the expression does not match. I choose NOT FOUND because it is easy for me to detect and see the sentence in the request and know easily if the expression is not matching.

Let's log the value that will be obtained by the regular expression after running the script by adding a JSR223 PostProcessor:

So, we can see the regular expression is working well and is saving the token in the variable defined:

Now you can work with the extracted variable in your script and use it in your requests (bodies and headers). You just have to invoke the variable as usual in JMeter, by typing ${XSRF-TOKEN}.

Suppose we also want to extract the laravel_session value. Well, the way to do it is the same.

Let's add a new regular expression extractor:

Selecting the options explained before:

  • Field to check: Response Header.
  • Name of created variable: laravel_session (you can choose the name you want).
  • Regular Expression: Set-Cookie: laravel_session=(.*?);
  • Template: $1$
  • Match No: 1.
  • Default Value: NOT FOUND.

Now log the value of the defined variable, as we have before:

You should see the value logged in the JMeter console:

2018-07-06 16:15:48,525 INFO o.a.j.e.StandardJMeterEngine: Running the test!
2018-07-06 16:15:48,526 INFO o.a.j.s.SampleEvent: List of sample_variables: []
2018-07-06 16:15:48,530 INFO o.a.j.g.u.JMeterMenuBar: setRunning(true, *local*)
2018-07-06 16:15:48,655 INFO o.a.j.e.StandardJMeterEngine: Starting ThreadGroup: 1 : Blazedemo Login
2018-07-06 16:15:48,656 INFO o.a.j.e.StandardJMeterEngine: Starting 1 threads for group Blazedemo Login.
2018-07-06 16:15:48,656 INFO o.a.j.e.StandardJMeterEngine: Thread will start next loop on error
2018-07-06 16:15:48,656 INFO o.a.j.t.ThreadGroup: Starting thread group... number=1 threads=1 ramp-up=1 perThread=1000.0 delayedStart=false
2018-07-06 16:15:48,656 INFO o.a.j.t.ThreadGroup: Started thread group number 1
2018-07-06 16:15:48,656 INFO o.a.j.e.StandardJMeterEngine: All thread groups have been started
2018-07-06 16:15:48,657 INFO o.a.j.t.JMeterThread: Thread started: Blazedemo Login 1-1
2018-07-06 16:15:51,527 INFO o.a.j.e.JSR223PostProcessor: ***************************eyJpdiI6Ikh1eWlEaWVkaTdLWExFbW1TaFJPV1E9PSIsInZhbHVlIjoiUzk3N2N2SG9WaUtrVkRWdzdXdEE0ZzdUU285cm92aHdIaDFGd3pCWTFqWGtKZHdsMVwvbzhrZmZpVWFIUjJJTVNOV2MrMEpcLzhkZks3Y3ZXa0lHcjkrUT09IiwibWFjIjoiYmI1YjFhZDE3MTg1ODA5MTVkNWM4MzFmYzFlZWZkOTdiYTAzZWM3MjJhYTkyNDEwZTc0OWI3NGVlZGYxZWU3MCJ9
2018-07-06 16:15:51,528 INFO o.a.j.t.JMeterThread: Thread is done: Blazedemo Login 1-1
2018-07-06 16:15:51,528 INFO o.a.j.t.JMeterThread: Thread finished: Blazedemo Login 1-1
2018-07-06 16:15:51,531 INFO o.a.j.e.StandardJMeterEngine: Notifying test listeners of end of test
2018-07-06 16:15:51,531 INFO o.a.j.g.u.JMeterMenuBar: setRunning(false, *local*)

Again, you just have to invoke the variable as is usual in JMeter, typing ${laravel_session}.

There are other options if the value you want to handle is a cookie. Please check out this article. I hope working with these values will be a little easier for you now! Let us know in the comments.

Extract Session (web analytics)

Published at DZone with permission of Leticia Almeida, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • A Beginner's Guide to Spark UI: Concepts and How to Use It
  • Multi-Tenant .NET Applications With Keycloak Realms
  • Observability and DevTool Platforms for AI Agents

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!