Contents

Azure Load Testing Series - Accessing bearer token in Jmeter using JSON extractor

About this blog

Hey folks!, In this blog we will see how to pass bearer token dynamically while running the test plans.


Mock API

In order to cover the scenarios, I have used a mock API that exposes token endpoint and a dummy GET endpoint.


Extract token within a thread group using JSON Extractor

Configuring GET endpoint with HTTP Header manager

  • Create ThreadGroup and Add HTTP request , ThreadGroups->Add->Sampler->HTTP Request

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/walpuurlk6scxh5hmeua.png

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sm8kmi9lgjgcbhwqc8c2.png

  • Above GET endpoint requires an bearer token to authorize the call.

  • We can add the headers by adding config element under the HTTP Request option menu

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fdsjdamhq7hk3gbnsk9.png

  • In the HTTP header manager add authorization key value as below
1
Authorization : Bearer ${auth_token}

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mq6heqdlh0z6drvs0hvr.png

  • ${auth_token} - We need to somehow get this bearer token dynamically to make sure we send the valid authorization header along with the GET endpoint

Configuring Token endpoint

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n66q6i5dnqdvbuno4foc.png

  • Add below key vaule in the Headers manager
1
content-type:application/x-www-form-urlencoded

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nyhwv6xxi2gh3gv1rg0r.png

  • Request body we will send along with the POST call are
    • grant_type : password
    • username : Steve Smith
    • password : p$ssw0rd
    • client_id : test
    • client_secret : 99ae8af9-cf8a-4cb3-89b2-9a42b97762b2

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjgxung9875x8vkorlwk.png

  • Add listener view result from HTTP request -> Add -> Listener -> View result tree

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0ohpoevygq4hzfoza3d.png

  • Now click on Run for the Get access token endpoint and you can see the bearer token generated in the Response body

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5pa2aqpedbwwd0kzscf.png

Create JSON extractor

  • In order to make use of this token generated in other HTTP requests create a JSON extractor from HTTP Request->Add->Post Processors->JSON Extractor

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77t61zdu1h6df20s6i1n.png

  • Provide “Names of created variables” and JSON Path expression as below
1
2
Names of create variables : auth_token
JSON Path expressions: $.access_token

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3yvf6ufe2qytmi0phxd.png

  • Now we can pass on “auth_token” variable in another HTTP request within the same Thread Group

Request Headers

  • Now we can see that the bearer token in the request headers

https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s38vzyg4wpvzvnof9lpu.png


Next steps

In our next blog we will see how to pass access token dynamically between different thread groups


Join us

Please go ahead and join our discord channel (https://discord.gg/8Cs82yNS) to give some valuable feedbacks