如何在通过 Beanshell 预处理器发送 JMeter 之前修改 HTTP 请求?

How to modify HTTP request before sending in JMeter through Beanshell pre processor?

我的 csv 文件中有测试用例。请求 URL 有一个 custom 变量。

示例 URL:.../abc/$id

我需要将此 id 替换为我们从上一个请求得到的响应 id。我使用 json 提取器从响应中获取 id。现在我需要在下一个测试用例请求中更新此 id。使用以下代码从 jmeter 上下文中获取请求 URL:

String path = ctx.getCurrentSampler().toString(); 
path.replaceAll("$id", id);

我无法在 jmeter 上下文中设置此更新 URL (ctx)

  1. 您需要为 path 变量分配新的路径值
  2. 您需要使用 sampler.setPath() method
  3. 将采样器路径设置为新值

所以你需要像这样修改你的代码:

String path = ctx.getCurrentSampler().toString();
path = path.replaceAll("$id", id);
sampler.setPath(path);

演示:


还可以考虑切换到 JSR223 PreProcessor and Groovy language as Groovy performance is much higher, it better supports new Java features and provides some extra "syntax sugar" on top. See Groovy is the New Black 文章了解详细信息。

尽可能避免使用预/post 处理器。 你的要求很简单直接。

直接在路径中使用它 - 假设 id 是具有值的变量的名称。

/abc/${id}