51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

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

英文:

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

问题 {#heading}

我正在尝试计算一个变量的值,以便将该值作为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](http://static.51tbox.com/static/2024-11-29/col/1a186d47c9d5642c87f75468603628c9/0fa55de13081485a9b403b47d01da72a.png.jpg)\]http://static.51tbox.com/static/2024-11-29/col/1a186d47c9d5642c87f75468603628c9/0fa55de13081485a9b403b47d01da72a.png.jpg


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


\</details\>


答案1
===



得分: 1




1. 
   这个 `DateUtils.toIso8601Date` 函数是我不熟悉的,它既不在http://static.51tbox.com/static/2024-11-29/col/1a186d47c9d5642c87f75468603628c9/0fa55de13081485a9b403b47d01da72a.png.jpg中,也不在随JMeter一起提供的库中。如果你正在使用一个自定义库来提供这个函数,你需要:




   * 将它复制到https://jmeter.apache.org/usermanual/get-started.html#classpath


   * 重新启动JMeter以加载它


   * 添加一个https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html语句来加载它





2. 
   https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting,特别是涉及到"重型"任务,比如加密操作时,请考虑迁移。更多信息: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 http://static.51tbox.com/static/2024-11-29/col/1a186d47c9d5642c87f75468603628c9/0fa55de13081485a9b403b47d01da72a.png.jpg 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 https://jmeter.apache.org/usermanual/get-started.html#classpath


   * restart JMeter to pick it up


   * add an https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html statement to load it





2. 
   https://jmeter.apache.org/usermanual/best-practices.html#bsh_scripting especially when it comes to \&quot;heavy\&quot; tasks like cryptographic operations so consider migrating. More information: https://www.blazemeter.com/blog/beanshell-vs-jsr223-vs-jmeter







\</details\>


赞(0)
未经允许不得转载:工具盒子 » 如何计算一个变量并将其作为标题发送到jmeter