如何在 Beanshell 预处理器中获取 HTTP POST 请求体?

How to get HTTP POST request body in Beanshell Preprocessor?

我在使用 jmeter 时遇到了一些问题。这是我的用例,我使用 CSV 数据源参数构造一个 HTTP POST 请求,请求正文是从 CSV 列

中读取的

其中包含一些占位符,例如 ${source_id}

我想用我通过 regex/json 提取器初始化的 jmeter 参数替换这些占位符(从上次 PUT 请求的响应中读取)。我尝试在 CSV 文件中使用 jmeter 变量名,但变量值没有被替换。我想我将不得不使用 beanshell 预处理器来修改 HTTP POST 请求主体。谁能帮助我获取 HTTP POST 请求正文的方法。

类似

String requestBody = sampler.getArguments().getArgument(0).getValue();

应该有帮助。

sampler 是一个 shorthand 到父采样器 class 实例,在您的情况下它将是 HTTPSamplerProxy,请参阅 JavaDoc 以了解所有可用的方法和字段。


我建议考虑迁移到 JSR223 PreProcessor and Groovy language as it is much faster and less resources consuming than Beanshell. Valid Beanshell code will be valid Groovy code so you should be able to convert to JSR223 elements with no or minimal changes. See Groovy is the New Black 文章以获取更多详细信息。