You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JMeter中使用预处理器将JSON请求存储到变量并以Base64值传递给后续请求

How to Store Dynamic JSON in a Variable and Pass It as Base64 in JMeter

Got it, let's walk through implementing this scenario step by step. I'll break it down into clear, actionable steps with code examples and best practices.


Prerequisites

First, make sure you have a CSV Config Element set up correctly to read your input values. Place it at the Thread Group level (or before your test elements) so its variables are available when building the JSON.


Step 1: Build and Store Dynamic JSON in a Variable via JSR223 Preprocessor

We'll use a JSR223 Preprocessor to construct the JSON request body using CSV values, then save it to a variable (e.g., foo).

  1. Add a JSR223 Preprocessor as a child of your target HTTP Request (or at the Thread Group level if you need it for multiple requests).
  2. Paste this Groovy script into the preprocessor (adjust the JSON structure and CSV variable names to match your use case):
    // Fetch values from CSV Config Element (replace with your CSV column names)
    def username = vars.get("username")
    def email = vars.get("email")
    def accountId = vars.get("accountId")
    
    // Build your JSON request body using the CSV values
    def jsonRequestBody = """
    {
      "user": {
        "username": "${username}",
        "email": "${email}",
        "account_id": "${accountId}"
      },
      "metadata": {
        "source": "jmeter_test",
        "timestamp": "${__time(yyyy-MM-dd'T'HH:mm:ss.SSSZ,)}"
      }
    }
    """
    
    // Store the complete JSON string in a variable named "foo"
    vars.put("foo", jsonRequestBody)
    
  3. Key notes:
    • Use vars.get("csv_variable_name") to pull values from your CSV Config.
    • The ${__time(...)} function adds a dynamic timestamp (optional, but useful for unique requests).
    • vars.put("foo", jsonRequestBody) saves the full JSON to the foo variable, which we'll use later.

Step 2: Pass the JSON Variable as Base64 in Your HTTP Request

You have two straightforward options to encode the foo variable to Base64 and send it in your request:

Option 1: Use JMeter's Built-in __base64Encode Function

This is the simplest approach if you only need the Base64 value in one place:

  1. In your HTTP Request's Body Data section, paste:
    ${__base64Encode(${foo},)}
    
  2. How it works: The __base64Encode function takes the value of foo (your full JSON) and encodes it to Base64 directly in the request body.

Option 2: Encode in the JSR223 Preprocessor (Reusable Value)

If you need the Base64-encoded string in multiple requests or test elements, encode it once in the preprocessor and save it to another variable:

  1. Update your JSR223 Preprocessor script with this additional code:
    // ... (keep the existing code to build jsonRequestBody) ...
    
    // Encode the JSON to Base64
    import java.util.Base64
    def base64EncodedJson = Base64.getEncoder().encodeToString(jsonRequestBody.getBytes("UTF-8"))
    
    // Save the encoded string to a new variable
    vars.put("foo_base64", base64EncodedJson)
    
  2. In your HTTP Request's Body Data, just reference the new variable:
    ${foo_base64}
    

Troubleshooting Tips

  • Verify CSV values are loaded: Add a Debug Sampler to your test plan to check if your CSV variables and the foo variable contain the expected values.
  • Validate JSON structure: Use the ${__eval(${foo},)} function in a Debug Sampler to see the fully resolved JSON string (ensuring CSV values are correctly inserted).
  • Check Base64 encoding: Decode the Base64 string manually (using an online tool or code) to confirm it matches your original JSON.

内容的提问来源于stack exchange,提问作者Mahaveer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:32:39