JMeter新手求助:如何从WebSocket-Request-Response-Sampler中提取agent_name字段
Hey there! Let's work through how to extract that agent_name field from your WebSocket-Request-Response-Sampler. Since you're new to JMeter, I'll break this down into easy, actionable steps so you can get it working quickly.
This is the simplest method if your WebSocket response follows a JSON structure (matching your request format):
- Right-click your WebSocket-Request-Response-Sampler, then select
Add > Post Processors > JSON Extractor - Configure the extractor with these settings:
- Reference Name: Enter a variable name for later use (e.g.,
extracted_agent_name— you can reference it as${extracted_agent_name}in other test elements) - JSON Path Expression: Use
$.agent_name(this targets theagent_namefield at the root level of the JSON response) - Match No. (0 for Random): Input
1to grab the first (and only, in your case) matching instance - Leave the remaining settings as default, then save your changes
- Reference Name: Enter a variable name for later use (e.g.,
If you want to stick with the JSR223 Post Processor, the issue is likely how you're accessing the WebSocket response data. Here's the correct Groovy script (Groovy is JMeter's recommended language for JSR223 elements due to better performance):
- Right-click your sampler, add
Add > Post Processors > JSR223 Post Processor - Paste this into the Script box:
// Grab the raw WebSocket response as a string def responseContent = prev.getResponseDataAsString() // Parse the JSON response def jsonResponse = new groovy.json.JsonSlurper().parseText(responseContent) // Extract agent_name and store it in a JMeter variable vars.put("extracted_agent_name", jsonResponse.agent_name) - Common pitfalls that might have broken your original attempt:
- Using the wrong method to fetch the response (always use
prev.getResponseDataAsString()to access sampler response content) - Typos in the field name (double-check that
agent_namematches exactly what's in the response, including capitalization) - Overlooking that the response might not be pure JSON (verify the structure via the "Response Data" tab of your sampler)
- Using the wrong method to fetch the response (always use
If you're actually trying to pull the agent_name from the request you sent (instead of the response), use a JSR223 Pre Processor (runs before the sampler sends the request):
- Add
Add > Pre Processors > JSR223 Pre Processorto your sampler - Use this script:
// Get the request body content def requestBody = sampler.getArguments().getArgument(0).getValue() // Parse the request JSON def jsonRequest = new groovy.json.JsonSlurper().parseText(requestBody) // Store the agent_name in a variable vars.put("request_agent_name", jsonRequest.agent_name)
内容的提问来源于stack exchange,提问作者MAX GRIMM

