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

如何从响应中提取属性写入配置?含带引号GUID提取需求

嘿,我来帮你解决这两个问题,分常用场景给你具体操作方法:

问题1:从响应中提取属性并写入properties配置

这里分几种主流工具的实现方式,你可以根据自己的使用场景选择:

Postman + Newman(接口测试场景)

  • 先在请求的Tests标签页编写脚本,解析响应并提取目标属性:
    // 假设响应是JSON格式,比如 {"userId": 123, "token": "abc123"}
    const responseData = pm.response.json();
    // 提取token属性
    const token = responseData.token;
    // 先把值存入环境变量
    pm.environment.set("extracted_token", token);
    
  • 如果要写入本地config.properties文件,需要用Newman运行时导出变量,再搭配简单Node.js脚本转换格式:
    // 示例node脚本:convertToProperties.js
    const fs = require('fs');
    const jsonData = require('./newman-report.json');
    const propertiesContent = `extracted_token=${jsonData.run.environment.values.find(v => v.key === 'extracted_token').value}`;
    fs.writeFileSync('config.properties', propertiesContent);
    
    运行Newman时加上--reporters json生成报告,再执行这个脚本即可完成写入。

JMeter(性能测试场景)

  • 添加JSON Extractor作为请求的子节点,配置:
    • Reference Name:比如extracted_token
    • JSON Path Expression:比如$.token(对应响应里的token属性)
  • 添加Properties File Writer组件,配置:
    • Filename:比如./config.properties
    • Property Names:填你提取的变量名extracted_token
      运行JMeter后,提取的值会自动写入指定的properties文件。

Java代码(开发场景)

  • 用Jackson解析响应,提取属性后写入properties:
    import com.fasterxml.jackson.databind.JsonNode;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.io.FileOutputStream;
    import java.util.Properties;
    
    public class ExtractAndWriteProperties {
        public static void main(String[] args) throws Exception {
            // 假设响应字符串
            String response = "{\"userId\": 123, \"token\": \"abc123\"}";
            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.readTree(response);
            String token = rootNode.get("token").asText();
    
            // 写入properties
            Properties props = new Properties();
            props.setProperty("extracted.token", token);
            props.store(new FileOutputStream("config.properties"), "Extracted from response");
        }
    }
    

问题2:提取不带引号的GUID并写入properties

你的响应是带双引号的GUID字符串(比如"f42210c4-2c67-44b2-af6c-07021ed83743"),核心是去掉首尾双引号再写入,分场景操作如下:

Postman + Newman

  • 在Tests脚本里直接处理响应文本:
    // 获取原始响应文本
    const rawResponse = pm.response.text();
    // 去掉首尾的双引号
    const guid = rawResponse.replace(/^"|"$/g, '');
    // 存入环境变量
    pm.environment.set("extracted_guid", guid);
    
    后续写入properties的步骤和问题1的Newman脚本一致,把变量名换成extracted_guid即可。

JMeter

  • 添加正则表达式提取器作为请求的子节点,配置:
    • Reference Name:extracted_guid
    • Regular Expression:"(.+?)"
    • Template:$1$(取第一个分组,也就是引号中间的内容)
  • 同样用Properties File Writer写入,变量名填extracted_guid。

Java代码

  • 直接处理字符串去掉引号,再写入properties:
    import java.io.FileOutputStream;
    import java.util.Properties;
    
    public class ExtractGuidToProperties {
        public static void main(String[] args) throws Exception {
            // 假设响应字符串
            String response = "\"f42210c4-2c67-44b2-af6c-07021ed83743\"";
            // 去掉首尾的双引号
            String guid = response.substring(1, response.length() - 1);
            // 也可以用replace:response.replace("\"", "")
    
            // 写入properties
            Properties props = new Properties();
            props.setProperty("extracted.guid", guid);
            props.store(new FileOutputStream("config.properties"), "Extracted GUID");
        }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:12:05