1

我正在尝试使用 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?我已经搜索但找不到任何有效的示例。

谢谢!

4

2 回答 2

2

从 1.0.0 版本开始,您可以处理 URI 中带有编码字符的请求。一个例子是:

def result = HttpBuilder.configure {
    request.raw = "http://localhost:8080/projects/myteam%2Fmyrepo/myfile.json"
}.get()

注意,在示例中使用raw而不是uri。使用这种方法需要您自己对 Uri 进行任何其他编码/解码。

于 2017-09-27T12:25:57.557 回答
0

可能的解决方法

Gitlab API 允许您通过项目 id 或项目名称进行查询。先查项目id,再查询项目。

  1. 首先查找项目ID。请参阅https://docs.gitlab.com/ee/api/projects.html#list-all-projects

    def projects = // 获取 /projects

    def project = projects.find { it['path_with_namespace'] == 'diaspora/diaspora-client' }

  2. 通过 :id 获取项目,请参阅https://docs.gitlab.com/ee/api/projects.html#get-single-project

    获取 /projects/${project.id}

于 2017-09-11T17:24:38.963 回答