如何计算一个变量并将其作为标题发送到jmeter

huangapple go评论102阅读模式
英文:

How to calculate a variable and send it as a header in jmeter

问题

我正在尝试计算一个变量的值以便将该值作为JMETER头部发送类似于这样

```java
Date timestamp = new Date();
String now = DateUtils.toIso8601Date(timestamp);
vars.put("timestamp", "hello");

我将此代码放在一个beanshell预处理器中,然后在头部管理器中使用以下语法,希望将时间戳作为头部发送:

如何计算一个变量并将其作为标题发送到jmeter

然而,变量的值并未在头部替换。在接下来的步骤中,我希望能够使用代码来计算HMAC签名并将其作为头部发送。在jmeter中是否有任何方法可以实现这一点?可以使用脚本来计算头部值,然后发送吗?我在postman中使用了预先脚本来做到这一点,但在jmeter中我没有看到类似的机制。
谢谢


<details>
<summary>英文:</summary>

I am trying to calculate a variable value in order to send that value as a header in JMETER, something like this:

Date timestamp = new Date();
String now = DateUtils.toIso8601Date(timestamp);
vars.put("timestamp", "hello");


I put this code in a beanshell preprocessor and then used the following syntax in a header manager hoping to send the timestamp as a header:

[![enter image description here][1]][1]

However the value of the variable is not being replaced in the header. Further down the line I want to be able to use code to calculate an HMAC signature and send it as a header. Is there any way to do this in jmeter? use a script to calculate a header value and then send it? I&#39;ve done this in postman with the pre-req scripts but I don&#39;t see a similar mechanism in jmeter. 
Thanks


  [1]: https://i.stack.imgur.com/aFARX.png

</details>


# 答案1
**得分**: 1

1. 这个 `DateUtils.toIso8601Date` 函数是我不熟悉的,它既不在[Java SDK][1]中,也不在随JMeter一起提供的库中。如果你正在使用一个自定义库来提供这个函数,你需要:

   - 将它复制到[JMeter类路径][2]
   - 重新启动JMeter以加载它
   - 添加一个[导入][3]语句来加载它

2. [自从JMeter 3.1版本开始,推荐使用JSR223测试元素和Groovy语言进行脚本编写][4],特别是涉及到“重型”任务,比如加密操作时,请考虑迁移。更多信息:[Beanshell vs. JSR223 vs. Java For JMeter: 完整对比][5]

   [1]: https://docs.oracle.com/en/java/javase/11/docs/api/index.html
   [2]: https://jmeter.apache.org/usermanual/get-started.html#classpath
   [3]: https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
   [4]: https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting
   [5]: https://www.blazemeter.com/blog/beanshell-vs-jsr223-vs-jmeter

<details>
<summary>英文:</summary>

 1. This `DateUtils.toIso8601Date` function is not something I&#39;m aware of, it&#39;s neither present in [Java SDK][1] nor in libraries which come with JMeter. If you&#39;re using a custom library which provides this function you will need to:

    - copy it to [JMeter Classpath][2]
    - restart JMeter to pick it up
    - add an [import][3] statement to load it 
 2. [Since JMeter 3.1 it&#39;s recommended to use JSR223 Test Elements and Groovy language for scripting][4] especially when it comes to &quot;heavy&quot; tasks like cryptographic operations so consider migrating. More information: [Beanshell vs. JSR223 vs. Java For JMeter: Complete Showdown][5]


  [1]: https://docs.oracle.com/en/java/javase/11/docs/api/index.html
  [2]: https://jmeter.apache.org/usermanual/get-started.html#classpath
  [3]: https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
  [4]: https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting
  [5]: https://www.blazemeter.com/blog/beanshell-vs-jsr223-vs-jmeter

</details>



huangapple
  • 本文由 发表于 2023年8月11日 03:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定