我正在尝试使用 Groovy 编写对我们 GitLab 服务器的 GET 请求的脚本以检索文件。API URI 格式为:
https://githost/api/v4/projects/<namespace>%2F<repo>/files/<path>?ref=<branch>
请注意,命名空间和 repo 之间有一个编码的“/”。最终的 URI 需要如下所示才能正常工作:
https://githost/api/v4/projects/mynamespace%2Fmyrepo/files/myfile.json?ref=master
我有以下代码:
File f = HttpBuilder.configure {
request.uri.scheme = scheme
request.uri.host = host
request.uri.path = "/api/v4/projects/${apiNamespace}%2F${apiRepoName}/repository/files/${path}/myfile.json"
request.uri.query.put("ref", "master")
request.contentType = 'application/json'
request.accept = 'application/json'
request.headers['PRIVATE-TOKEN'] = apiToken
ignoreSslIssues execution
}.get {
Download.toFile(delegate as HttpConfig, new File("${dest}/myfile.json"))
}
但是,%2F 被重新编码为 %252F。我已经尝试了多种方法来尝试创建 URI,以便它不会在命名空间和 repo 之间对 %2F 进行编码,但我无法得到任何工作。它要么重新编码“%”,要么将其解码为文字“/”。
如何使用 Groovy + http-builder-ng 以保留编码的“/”的方式设置 URI?我已经搜索但找不到任何有效的示例。
谢谢!