# 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. - Mock API - Mock Oauth Token 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 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/walpuurlk6scxh5hmeua.png) - Create HTTP Request for GET endpoint : https://httpbin.org/anything/foo ![Image description](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 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fdsjdamhq7hk3gbnsk9.png) - In the HTTP header manager add authorization key value as below ```cmd Authorization : Bearer ${auth_token} ``` ![Image description](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 - Now we need to follow the same steps of adding HTTP request and HTTP header manager as we did before for the POST endpoint : https://virtserver.swaggerhub.com/helen/oauth-token-mock/1.0.0/token ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n66q6i5dnqdvbuno4foc.png) - Add below key vaule in the Headers manager ```cmd content-type:application/x-www-form-urlencoded ``` ![Image description](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 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gjgxung9875x8vkorlwk.png) - Add listener view result from HTTP request -> Add -> Listener -> View result tree ![Image description](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 ![Image description](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 ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77t61zdu1h6df20s6i1n.png) - Provide "Names of created variables" and JSON Path expression as below ```cmd Names of create variables : auth_token JSON Path expressions: $.access_token ``` ![Image description](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 ![Image description](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 ## Sponsor