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

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.

Option 1: Use the JSON Extractor (Beginner-Friendly)

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 the agent_name field at the root level of the JSON response)
    • Match No. (0 for Random): Input 1 to grab the first (and only, in your case) matching instance
    • Leave the remaining settings as default, then save your changes
Option 2: Fix Your JSR223 Post Processor

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_name matches 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)
Bonus: Extract from the Request Body (If Needed)

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 Processor to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 10:52:31