如何在Postman中过滤Confluence空间API返回结果
问题
我在Postman中执行GET API请求:https://[myDomain].atlassian.net/wiki/rest/api/space/,得到了如下返回结果,但希望对结果进行过滤,仅保留id、key、name、homepage和webui字段的值,请问如何在Postman中实现这一需求?
{ "results": [ { "id": 98430, "key": "DOC", "name": "Documents", "type": "global", "status": "current", "_expandable": { "settings": "/rest/api/space/DOC/settings", "metadata": "", "operations": "", "lookAndFeel": "/rest/api/settings/lookandfeel?spaceKey=DOC", "identifiers": "", "permissions": "", "icon": "", "description": "", "theme": "/rest/api/space/DOC/theme", "history": "", "homepage": "/rest/api/content/98633" }, "_links": { "webui": "/spaces/DOC", "self": "https://xxxxxx.atlassian.net/wiki/rest/api/space/DOC" } }, { "id": 425986, "key": "~63be918f98bf50328c68aec2", "name": "MyDocs", "type": "personal", "status": "current", "_expandable": { "settings": "/rest/api/space/~63be918f98bf50328c68aec2/settings", "metadata": "", "operations": "", "lookAndFeel": "/rest/api/settings/lookandfeel?spaceKey=~63be918f98bf50328c68aec2", "identifiers": "", "permissions": "", "icon": "", "description": "", "theme": "/rest/api/space/~63be918f98bf50328c68aec2/theme", "history": "", "homepage": "/rest/api/content/426171" }, "_links": { "webui": "/spaces/~63be918f98bf50328c68aec2", "self": "https://xxxxxx.atlassian.net/wiki/rest/api/space/~63be918f98bf50328c68aec2" } }, { "id": 2064386, "key": "~5f7af04cb61f66006f28fafc", "name": "Content Management", "type": "personal", "status": "current", "_expandable": { "settings": "/rest/api/space/~5f7af04cb61f66006f28fafc/settings", "metadata": "", "operations": "", "lookAndFeel": "/rest/api/settings/lookandfeel?spaceKey=~5f7af04cb61f66006f28fafc", "identifiers": "", "permissions": "", "icon": "", "description": "", "theme": "/rest/api/space/~5f7af04cb61f66006f28fafc/theme", "history": "", "homepage": "/rest/api/content/2064576" }, "_links": { "webui": "/spaces/~5f7af04cb61f66006f28fafc", "self": "https://xxxxxx.atlassian.net/wiki/rest/api/space/~5f7af04cb61f66006f28fafc" } }, { "id": 98306, "key": "~5f7aef9c8d88b300751faba5", "name": "AI Development", "type": "personal", "status": "current", "_expandable": { "settings": "/rest/api/space/~5f7aef9c8d88b300751faba5/settings", "metadata": "", "operations": "", "lookAndFeel": "/rest/api/settings/lookandfeel?spaceKey=~5f7aef9c8d88b300751faba5", "identifiers": "", "permissions": "", "icon": "", "description": "", "theme": "/rest/api/space/~5f7aef9c8d88b300751faba5/theme", "history": "", "homepage": "/rest/api/content/98389" }, "_links": { "webui": "/spaces/~5f7aef9c8d88b300751faba5", "self": "https://xxxxxx.atlassian.net/wiki/rest/api/space/~5f7aef9c8d88b300751faba5" } }, { "id": 229380, "key": "SD", "name": "Software Development", "type": "global", "status": "current", "_expandable": { "settings": "/rest/api/space/SD/settings", "metadata": "", "operations": "", "lookAndFeel": "/rest/api/settings/lookandfeel?spaceKey=SD", "identifiers": "", "permissions": "", "icon": "", "description": "", "theme": "/rest/api/space/SD/theme", "history": "", "homepage": "/rest/api/content/229464" }, "_links": { "webui": "/spaces/SD", "self": "https://xxxxxx.atlassian.net/wiki/rest/api/space/SD" } } ], "start": 0, "limit": 25, "size": 5, "_links": { "base": "https://xxxxxx.atlassian.net/wiki", "context": "/wiki", "self": "https://xxxxxx.atlassian.net/wiki/rest/api/space/" } }
解决方案
你可以通过Postman的Tests脚本处理API响应,过滤出指定字段,具体操作步骤如下:
1. 打开请求的Tests标签页
在Postman中打开该GET请求,切换到顶部的「Tests」标签页。
2. 编写过滤脚本
将以下JavaScript代码粘贴到Tests编辑器中,代码会提取你需要的字段并重构响应数据:
// 获取原始响应的JSON数据 const rawData = pm.response.json(); // 遍历results数组,提取目标字段 const filteredResults = rawData.results.map(item => ({ id: item.id, key: item.key, name: item.name, homepage: item._expandable.homepage, webui: item._links.webui })); // 构造过滤后的响应结构(可按需保留分页信息) const filteredData = { results: filteredResults, start: rawData.start, limit: rawData.limit, size: rawData.size }; // 替换Postman显示的响应内容,直接展示过滤后的数据 pm.response.toJSON = function() { return { code: pm.response.code, status: pm.response.status, headers: pm.response.headers, response: filteredData }; }; // 可选:将过滤结果存入环境变量,供后续请求使用 pm.environment.set("filteredSpaces", JSON.stringify(filteredData)); // 可选:在控制台打印结果,方便调试 console.log("过滤后的空间数据:", filteredData);
3. 发送请求查看结果
点击「Send」发送请求后,切换到「Response」标签页,即可看到仅包含id、key、name、homepage和webui字段的过滤后数据。
注意事项
- 代码中
item._expandable.homepage和item._links.webui是根据你提供的响应结构提取的,若API返回结构有变化,需对应调整字段路径。 - 若不需要分页信息(start、limit、size),可直接将
filteredResults作为最终响应返回。
内容的提问来源于stack exchange,提问作者hitman126
相关产品推荐
相关产品推荐

