Generate and pass API token to subsequent request in JMETER

Step 1: Open JMeter -> Add Thread Group to the Test Plan -> Add Http request sampler

Step 2: Select HTTP Request sampler and change the name if you wish to.

Step 3: In the Basic tab Enter following information:

Protocol – http/https

Server Name or IP – Enter server name/IP to generate the token. e.g

HTTP Request – POST

Path: Enter the query parameter to generate the token. eg. oauth2/token

Step 4: In the parameter tab enter the following information:

Name: grant_type Value: client_credentials

Name: client_id Value: Enter client ID

Name: client_secret Value: Enter client secret

Step 5: Add Post Processor JSON Extractor in the Thread group.

Step 6: Enter following information:

Names of created variables: accessToken

JSON Path expression: access_token

Step 7: Add BeanShell Assertion in the thread group and add following code in the script section.

${__setProperty(bearerTokenforanotherThread, ${accessToken})};

Step 8: Add another thread group to add request which will be using the token generated in above steps.

Step 9: Add BeanShell PreProcessor in the Thread to store the token value and add following code in script section of BeanShell PreProcessor

String bearerToken = ${__property(bearerTokenforanotherThread)}.

Step 10: Add HttpRequest in the thread group and enter following information.

Protocol: http/https

Server Name or IP: Name or IP of API server


Path: URI Path of the request

Step 11: Select http request -> right click ->Add->config element->HTTP Header Manager
Step 12: Add following code in http header manager:
Name: Authorization
Value: Bearer ${__property(bearerTokenforanotherThread)

Step 13: Run the plan. Token should be generated and pass to the subsequent request in next thread group.