在JMeter中,参考值未与请求标头一起发送 [英] The reference value not send along with request headers in JMeter
问题描述
我在JMeter中有一个HTTP请求采样器,用于发送登录请求.
我必须从必须与第二个HTTP请求采样器的请求标头一起发送的HTTP请求的响应中获取access_token
.
登录请求的响应数据如下:
{状态":true,"access_token":"91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BDWIURAYD
正则表达式提取器
Reference Name: access_token
Regular expression: access_token":"(.*?)"
Template $1$
MatchNo:1
当我使用查看结果树-> RegExp测试检查此正则表达式时,我得到的结果如下:
匹配数:1
匹配[1] [0] = access_token:""91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9wisn 匹配[1] [1] = 91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.pv
我需要获取Match [1] [1].即access_token
值.
在第二个 HTTP请求采样器 的 HTTP标头管理器 中引用表达式的值:
Name: Authorization
Value : Bearer ${access_token}
现在,当我运行JMeter测试并观察查看结果树时,我可以看到第二个请求的响应是
全局身份验证失败
和请求标头:
连接:保持活动状态
问题在以下行中:我必须获取access_token
值(91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.91kLM68tdMBoDFRURArvdmwYgWV9Nr2sHYDwivTM.Arvdm_M68-BoDFRURArvdmwYgWV9Nr2sHYDwivTM
)而不是${access_token}
:
授权:不记名$ {access_token}
内容类型:application/json
内容长度:21
主机:本地主机
用户代理:Apache-HttpClient/4.5.3(Java/1.8.0_111)
如果不是给参考令牌,而是在HTTP Header Manager中对令牌进行硬编码,则响应成功.
注意::我已经检查了[解决方案
您的正则表达式提取器很可能由于多余的空间或换行符或其他原因而失败.
请注意,从Apache JMeter 3.0开始,您具有 JSON提取器,即设计用于使用
请参见使用JMeter和JSON Extractor 文章以获取更多信息.
除此之外,测试计划中其余的元素配置看起来还不错.
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?
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:
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.
这篇关于在JMeter中,参考值未与请求标头一起发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!