JMeter中修改加密响应字段后重新加密发起请求的方法问询
policyStatus Field in JMeter Before Re-Encryption Got it, I’ve run into this exact headache before when dealing with encrypted JSON payloads in JMeter. The problem with using __strReplace() is that it treats the payload as a raw string—if your JSON has extra whitespace, escaped quotes, or nested fields, the string replacement can fail unpredictably (which is why it only worked when your request was hardcoded).
Here’s a reliable, JSON-aware solution that will work every time:
Step 1: Extract the Decrypted Payload as JSON (Ditch Regex)
Instead of a Regular Expression Extractor, use JMeter’s JSON Extractor right after your decryption POST request:
- Set Reference Name to something like
decryptedPayload - Set JSON Path Expression to
$(this grabs the entire JSON object from the response) - Set Match No. to
1to get the first (and only) match.
This ensures you’re capturing the payload as a valid JSON structure, not a messy string.
Step 2: Modify policyStatus Using Groovy (Best Performance)
Add a JSR223 PreProcessor before your encryption POST request. Use Groovy (JMeter’s recommended scripting language) to parse the JSON, update the field, and prepare it for re-encryption:
import groovy.json.JsonSlurper import groovy.json.JsonBuilder // Parse the extracted JSON payload def slurper = new JsonSlurper() def payload = slurper.parseText(vars.get("decryptedPayload")) // Update the policyStatus field (use "2" if it's a string value instead of numeric) payload.policyStatus = 2 // Convert the modified JSON back to a string, then escape quotes for the outer request body def builder = new JsonBuilder(payload) def jsonString = builder.toString() def escapedJson = jsonString.replaceAll(/"/, /\\"/) // Store the modified, escaped payload in a new variable vars.put("modifiedPayload", escapedJson)
Alternative: Use JMeter’s Built-in JSON Functions (No Scripting)
If you prefer avoiding Groovy, you can use JMeter’s __jsonSet function directly to update the field, then escape quotes for the outer JSON:
- First, set the modified value:
${__jsonSet(${decryptedPayload}, policyStatus, 2, modifiedPayload)} - Escape the result for valid JSON embedding:
${__escapeHtml(${modifiedPayload}, escapedModifiedPayload)}
Step 3: Update Your Encryption Request Body
Replace your parameterized payload reference with the modified, escaped variable:
{ "isEncrypt": true, "payload": "${modifiedPayload}" }
Why This Works
By parsing the payload as JSON instead of manipulating it as a string, you’re guaranteed to target the exact policyStatus field—regardless of whitespace, field order, or other formatting changes in the response. This eliminates the fragility of regex or string replacement methods.
内容的提问来源于stack exchange,提问作者yngielkoh

