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

Postman中Aruba Central GET响应转JSON及环境变量配置问题

问题背景

我正在基于Excel表中的无线接入点(AP)序列号,通过Postman批量执行Aruba Central API配置操作:对每个AP先发送GET请求获取10项配置属性,再发送包含11项属性的POST请求完成重新配置。其中7项属性复用GET响应结果、2项取自Excel、2项为静态值。

问题描述

当前仅在复用GET响应属性时遇到问题:该GET请求({{base_url}}/configuration/v1/ap_settings_cli/{{SERIAL}})的响应虽标记为application/json,但实际是特殊格式的字符串数组,无法直接用于设置环境变量。我需要将其转换为标准JSON对象格式:

{
  "per-ap-settings": "ab:xz:1x:5y:3c:65",
  "hostname": "room-1-ap",
  "ip-address": "0.0.0.0 0.0.0.0 0.0.0.0 0.0.0.0 \"\"",
  "wifi0-mode": "access",
  "wifi1-mode": "access",
  "g-channel": "0 -127",
  "a-channel": "0 -127",
  "a-external-antenna": "0",
  "g-external-antenna": "0",
  "zonename": "_#ALL#_"
}

目前用ChatGPT生成的转换代码接近完成,但存在两个问题:

  • 转换后ip-address字段末尾的转义引号(\"\")丢失,变为"";
  • 环境变量设置语法错误,导致后续POST请求无法获取变量值。

当前使用的转换及设置代码如下:

// Get the response body as a JSON object
responseBody = pm.response.json();

// Initialize an empty object to store the JSON format
const jsonFormat = {};

// Iterate through each line in the response body array
responseBody.forEach(line => {
  // Remove extra spaces and split the line by space to get the key-value pair
  // Exclude escaped quotes from being split
  const [key, ...valueParts] = line.trim().split(/(?<!\\) /);

  // Join the value parts back together and add the key-value pair to the jsonFormat object
  jsonFormat[key] = valueParts.join(' ');
});

// Log the jsonFormat object to the console
console.log(jsonFormat);
console.log(jsonFormat["per-ap-settings"])
console.log(jsonFormat.hostname);
console.log(jsonFormat["ip-address"])
console.log(jsonFormat["wifi0-mode"])
console.log(jsonFormat["wifi1-mode"])
console.log(jsonFormat["g-channel"])
console.log(jsonFormat["g-channel"])

// If you want to set the converted JSON as an environment variable, uncomment the following line:
// pm.environment.set("convertedJson", JSON.stringify(jsonFormat));


pm.environment.set("hostname", pm.response.json().hostname);
pm.environment.set("per-ap-settings", pm.response.json["per-ap-settings"]);
pm.environment.set("ip-address", pm.response.json["ip-address"]);
pm.environment.set("wifi0-mode", pm.response.json["wifi0-mode"]);
pm.environment.set("wifi1-mode", pm.response.json["wifi1-mode"]);
pm.environment.set("g-channel", pm.response.json["g-channel"]);
pm.environment.set("a-channel", pm.response.json["a-channel"]);

后续POST请求示例:

{
  "clis": [
  "per-ap-settings {{per-ap-settings}}",
  "  hostname {{hostname}}",
  "  ip-address {{ip-address}}",
  "  wifi0-mode {{wifi0-mode}}",
  "  wifi1-mode {{wifi1-mode}}",
  "  g-channel {{g-channel}}",
  "  a-channel {{a-channel}}",
  "  a-external-antenna {{AGAIN}}",
  "  g-external-antenna {{GGAIN}}",
  "  zonename zoneA",
  "  rf-zone newsite"
  ]
}

解决方案

1. 修复转义引号丢失问题

原始响应解析后,字符串中的\"会被自动转为",直接拼接会丢失转义。需要在赋值时将双引号重新转义为\",修改值的处理逻辑:

const value = valueParts.join(' ').replace(/"/g, '\\"');

2. 修复环境变量设置错误

原代码错误地直接从原始响应对象中取值(原始响应是字符串数组,不存在这些属性),应该从转换后的jsonFormat对象中取值:

pm.environment.set("hostname", jsonFormat.hostname);
pm.environment.set("per-ap-settings", jsonFormat["per-ap-settings"]);
// 其他变量同理

完整修正代码

// 获取响应体的字符串数组
const responseBody = pm.response.json();

// 初始化存储转换结果的对象
const jsonFormat = {};

// 遍历每个配置行
responseBody.forEach(line => {
  // 去除首尾空格,按"非转义空格"分割键值对
  const [key, ...valueParts] = line.trim().split(/(?<!\\) /);
  
  // 拼接值部分,并将双引号重新转义
  const value = valueParts.join(' ').replace(/"/g, '\\"');
  jsonFormat[key] = value;
});

// 打印转换结果到控制台
console.log(jsonFormat);

// 设置环境变量
pm.environment.set("hostname", jsonFormat.hostname);
pm.environment.set("per-ap-settings", jsonFormat["per-ap-settings"]);
pm.environment.set("ip-address", jsonFormat["ip-address"]);
pm.environment.set("wifi0-mode", jsonFormat["wifi0-mode"]);
pm.environment.set("wifi1-mode", jsonFormat["wifi1-mode"]);
pm.environment.set("g-channel", jsonFormat["g-channel"]);
pm.environment.set("a-channel", jsonFormat["a-channel"]);

验证效果

转换后的ip-address字段会保留\"\",环境变量可被后续POST请求中的{{变量名}}正确引用,生成符合要求的CLI配置数组。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 21:16:08