1
votes

I have an HTTP Request sampler in JMeter which sends a login request.

I have to obtain the access_token from the response of this HTTP request which has to be sent along with the request header of the 2nd HTTP request Sampler.

The response data of login request is something like this:

{"status":true,"access_token":"91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM"}

Regular Expression Extractor

Reference Name: access_token
Regular expression: access_token":"(.*?)"
Template $1$
MatchNo:1

When I am checking this regular expression using View Results Tree->RegExp Test, I am obtaining a result like this:

Match count: 1

Match[1][0]= access_token":"91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM"

Match[1][1]= 91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM

What I need is to obtain Match[1][1]. ie just the access_token value.

Referring the value of the expression in my HTTP Header Manager of the 2nd HTTP request Sampler:

Name: Authorization
Value : Bearer ${access_token}

Now when I am running my JMeter test and observe the View Results Tree, I can see that the response of the second request is

Global Authentication Failed

and the Request Headers:

Connection: keep-alive

problem is in the following line: I have to get the access_token value (91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM) instead of ${access_token}:

Authorization: Bearer ${access_token}

Content-Type: application/json

Content-Length: 21

Host: localhost

User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_111)

If instead of giving the reference token I am hard-coding the token in HTTP Header Manager then the response is successful.

Note: I have already checked [How to get Response Data and parse it to HTTP Header Manager in JMeter

How can I solve this issue?

1
Does your Regex post-processor have a Default value set? If not, add it. That way you can identify whether Regex post-processor really not finding your regex (and thus will use default value), or it's not running at all (and thus you will still see ${access_token} unresolved. If regex is not found, there must be some mistake in regex extractor: check Apply To and Field To Check fields, make sure they are correct, maybe configure it again from scratch.Kiril S.
If default value is not returned, it means post-processor is not running at all. Then you need to check what sampler returns, whether it has other post-processors and so onKiril S.
@Kiril S. Thank u for the response, When am giving a default value the header manager takes that default value and that is passed along with the request header of the 2nd HTTP samplerNisha V

1 Answers

2
votes

Most likely your Regular Expression Extractor fails due to extra space or line break or whatever.

Be aware that since Apache JMeter 3.0 you have JSON Extractor which is designed for getting data from JSON response types using Json Path language. The relevant JSON Extractor configuration would be as simple as:

JMeter JSON Path Extractor

See API Testing With JMeter and the JSON Extractor article for more information on.

Apart from this, the remaining elements configuration in your Test Plan looks fine.