[英]How to extend Jmeter Http Sampler to inject auth token in request?
My requirement is I need to pick two http headers and few json fields in request body and using those values generate a auth token using Base64 encryption and set the auth Token into request body before Jmeter makes the HTTP request to the server. 我的要求是,我需要在请求正文中选择两个http标头和几个json字段,并使用这些值通过Base64加密生成一个auth令牌,并在Jmeter向服务器发出HTTP请求之前将auth令牌设置为请求正文。
How to do this in JMeter?? 如何在JMeter中执行此操作?
I tried creating custom functions in JMeter but in the Function class current Sampler object is null. 我尝试在JMeter中创建自定义函数,但在Function类中,当前的Sampler对象为null。
Thanks in advance. 提前致谢。
I believe that Beanshell PreProcessor is what you're looking for 我相信您正在寻找Beanshell PreProcessor
Access request headers: 访问请求标头:
sampler.getHeaderManager().get(0); // first header sampler.getHeaderManager().get(1); // second header
Access request body: 访问请求正文:
sampler.getArguments().getArgument(0).getValue();
Base64 Encode something: Base64编码:
import org.apache.jmeter.protocol.http.util.Base64Encoder; String encoded = Base64Encoder.encode("source");
For more information on Beanshell scripting in JMeter see How to use BeanShell: JMeter's favorite built-in component guide. 有关JMeter中Beanshell脚本编写的更多信息,请参见如何使用BeanShell:JMeter最喜欢的内置组件指南。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.