如何通过Confluence脚本从Confluence Wiki获取端点参数并调用带请求体的端点?
如何通过Confluence脚本从Confluence Wiki获取端点参数并调用带请求体的端点?
别慌,我给你一步步拆解实操流程,全程用Confluence生态里最常用的ScriptRunner插件来实现,这是Confluence做自定义脚本的核心工具:
第一步:准备工作 - 安装启用ScriptRunner
- 如果你的Confluence还没装ScriptRunner,先去插件市场搜索安装,启用后就能在Confluence里编写和运行Groovy脚本(Confluence脚本开发首选Groovy语言)。
第二步:在Confluence页面存储端点参数
先把端点的关键信息(URL、请求方法、请求头、请求体、认证信息等)规整到Confluence页面里,推荐用JSON格式的代码块来存储,方便脚本读取解析:
举个存储示例,在Confluence页面插入一个代码块(语言选择JSON),内容如下:
{ "endpointUrl": "https://your-api-domain.com/api/v1/submit", "method": "POST", "headers": { "Content-Type": "application/json", "Authorization": "Bearer your-auth-token" }, "requestBody": { "username": "test_user", "content": "confluence-script-test" } }
第三步:编写脚本读取Confluence页面的参数
接下来写Groovy脚本,读取目标页面的内容并解析出端点配置:
- 先找到目标页面的ID(从页面URL里提取,比如
pageId=123456里的数字就是页面ID) - 用Confluence的API获取页面内容,再提取并解析JSON配置:
import com.atlassian.confluence.pages.PageManager import com.atlassian.confluence.pages.Page import groovy.json.JsonSlurper // 获取Confluence的页面管理器实例 def pageManager = ComponentAccessor.getComponent(PageManager) // 替换成你的目标页面ID def targetPageId = 123456L Page targetPage = pageManager.getPage(targetPageId) // 提取页面中的JSON代码块内容 def pageContent = targetPage.bodyAsString // 匹配页面里的JSON代码块(如果有多个代码块,可根据实际调整匹配规则) def jsonCodeBlock = (pageContent =~ /<code class="language-json">(.*?)<\/code>/)[0][1] // 解析JSON配置 def slurper = new JsonSlurper() def endpointConfig = slurper.parseText(jsonCodeBlock) // 打印解析结果,验证是否正确读取 log.info("读取到的端点URL: ${endpointConfig.endpointUrl}") log.info("读取到的请求体: ${endpointConfig.requestBody}")
第四步:扩展脚本实现端点调用
在上面的脚本基础上,添加HTTP请求逻辑,用Groovy的RESTClient来简化请求操作(ScriptRunner默认支持该类):
import com.atlassian.confluence.pages.PageManager import com.atlassian.confluence.pages.Page import groovy.json.JsonSlurper import groovy.json.JsonBuilder import groovyx.net.http.RESTClient import static groovyx.net.http.ContentType.JSON // 前面的页面读取代码不变 def pageManager = ComponentAccessor.getComponent(PageManager) def targetPageId = 123456L Page targetPage = pageManager.getPage(targetPageId) def pageContent = targetPage.bodyAsString def jsonCodeBlock = (pageContent =~ /<code class="language-json">(.*?)<\/code>/)[0][1] def slurper = new JsonSlurper() def endpointConfig = slurper.parseText(jsonCodeBlock) // 初始化REST客户端 def client = new RESTClient(endpointConfig.endpointUrl) // 设置请求头 endpointConfig.headers.each { key, value -> client.defaultHeaders[key] = value } try { def response // 根据配置的请求方法调用端点 if (endpointConfig.method == "POST") { def bodyJson = new JsonBuilder(endpointConfig.requestBody).toString() response = client.post(body: bodyJson, contentType: JSON) } else if (endpointConfig.method == "PUT") { def bodyJson = new JsonBuilder(endpointConfig.requestBody).toString() response = client.put(body: bodyJson, contentType: JSON) } // 输出响应结果 log.info("请求成功!响应状态码: ${response.status}") log.info("响应内容: ${response.data}") } catch (Exception e) { // 捕获并输出异常信息 log.error("请求失败,原因: ${e.message}", e) }
第五步:运行脚本并验证
- 打开Confluence管理菜单里的「Script Console」(ScriptRunner的控制台入口)
- 把上面的脚本粘贴进去,替换成你的实际页面ID和端点配置
- 点击「Run」运行脚本,查看控制台日志,确认是否成功调用端点
关键注意事项
- 权限问题:运行脚本的用户需要有目标Confluence页面的读取权限,同时API端点要允许Confluence服务器的IP访问(避免防火墙或跨域拦截)
- 安全问题:不要在Confluence页面明文存储敏感信息(比如密码、Token),可以用ScriptRunner的加密属性功能,或者Confluence内置的安全存储来托管敏感数据
- 容错处理:可以给脚本添加更多异常捕获逻辑,比如页面不存在、JSON解析失败、请求超时等场景
备注:内容来源于stack exchange,提问作者vineeth sargant
相关产品推荐
相关产品推荐

