0
votes

I am new in JMeter. I am trying to set access token dynamically through "Regular Expression Extractor". Basically I am trying to first login and then from it's response , I am trying to get access token and set it in "CreatePost" API.

I am able to get access token using #1 Login API where response is as below.

{ "Message": "Login successfully.", "Status": "Success", "HttpStatus": 200, "Data": { "token_type": "Bearer", "expires_in": 10000, "access_token": "eyJ0eXAiE", "refresh_token": "2bcf1f455f2", "name": "jmeter_test", } }

Could you please help me to get access_token from above response. To get this , I have created "Regular Expression Extractor " and set

Name of Createdvariable :  access_token,
Regular Expression:        "access_token": "value"
Template                   $1$
Match No.                  1

Could yo please check the regular expression?

Thanks in advance.

2
Use JSON Path PostProcessor as expression: $.access_token OR $..access_token.Yugal

2 Answers

0
votes

If you really need to use the regular expression extractor you need to amend your regular expression to look like:

"access_token":\s+"(\w+)"

where:

  • (\w+) matches any number of aplhanumeric characters
  • \s+ matches optional whitespace character

Demo:

enter image description here

More information:


However for JSON responses it makes sense to use JSON Extractor which allows executing arbitrary JsonPath queries to get "interesting" data from the JSON responses, the relevant JsonPath expression will be:

$..access_token

where:

  • .. is "deep scan" operator, it will find the search term anywhere in the data
  • access_token - the JSON attribute you're looking for

enter image description here

More information: API Testing With JMeter and the JSON Extractor

1
votes

Use JSON Extractor as a child of the Sampler Returning Response with following JSON Path Expression:

$.Data.access_token

enter image description here

enter image description here