JMeter for login authentication

For logging any application, it requires different tokens whether be it an access token or refresh token or any, the most important thing is access token for login any users. So, for each login action, a new access token is generated and we need to gain that token in order or authentic login and accessing the application. Each time the server responds we require that response to store somewhere and access them through other APIs. 
This post will help you to fetch dynamic response and store response and access applications dynamically. All we need is a "Regular Expression Extractor" to extract token. So, let's get started with creating the scripts

Step 1: Add Thread Group
Step 2: Add Http Request 



Here you add the login credential and send the request to the server after sending the request, the server responds with an authorization token(as seen in the below screenshot) and other responses can be seen easily.


So, all we need for now is to extract the token part only. How can we do this? The answer is simple all we need is "Regular Expression Extractor"

Step 3: Add "Regular Expression Extractor"

In this section, you need to fill up the following parameters:
a. Name of Created Variable: Name of the variable in which the extracted text will be stored. In our example, it is ‘authorization’.
b. Regular Expression: It is used to parse the response data. This must contain at least one set of parentheses "()" to capture a portion of the string 
Suppose you need to match the following portion 
{"token":"eybdhsfbsdjfbdsjfds","user":"ABC"} and you want the section of token to extract. Regular Expression for that would be
"token": "(.+?)"
In our case its : "Authorization": "(.+?)"
The special character from above symbolizes
()- enclose the portion of the match string to be returned
. - match any character
+ - one or more times
? - stop when the first match succeeds
 
c. Template: Grouping of strings within ( ) brackets. $1$ means the 1st group, $2$ means 2nd group

d. Match Number: Tells which match should be picked. 0 is used for random.

e. Default Value: In case of no match which value should be picked. (can be empty)


Step 4: Add "HTTP Header Manager"


On every login, a token is stored and that token needs to be sent to other API to have access to the system. So, we need to send a piece of additional information to the HTTP Header Manager that is "token" so that all the responses are authorized
In our case the variable we used to extract the token was "authorization" and that will be used here 

If the token is not set on the HTTP Header Manager then we get the response as following


Step 5: Add all the APIs(Http Request)
Step 6: Run the scripts and you should see the response as follows: