cURL running from Gradle Exec task to get a file from GitHub API returns 404 while working as expected running in terminal

huangapple go评论60阅读模式
英文:

cURL running from Gradle Exec task to get a file from GitHub API returns 404 while working as expected running in terminal

问题

我有以下的Gradle Exec任务用于使用经典的个人访问令牌从GitHub下载文件(启用了所有repo范围):

tasks.register<Exec>("downloadScript") {

	val cmdLine = mutableListOf<String>("curl", "-H", "'Authorization: Bearer ${gh_token}'", "-H", "'Accept: application/vnd.github.raw'", "--remote-name", "-L", "https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py")

	commandLine(cmdLine)
} 

当我运行此任务时,我会得到这样的输出:

> Task :downloadScript FAILED
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0   123    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (22) The requested URL returned error: 404

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':downloadScript'.
> Process 'command 'curl'' finished with non-zero exit value 22 

这里最有趣的部分是,在终端中运行相同的命令(在IntelliJ本身或MacOS终端中)会成功下载文件,同时将${gh_token}替换为实际令牌。
curl -H 'Authorization: Bearer token_string' -H 'Accept: application/vnd.github.raw' --remote-name -L https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py

为什么将字符串传递给变量的commandline()导致命令行插值失败而使其失败呢?

英文:

I have the following Gradle Exec task to download a file from GitHub using a classic personal access token (all repo scopes enabled):

tasks.register<Exec>("downloadScript") {

	val cmdLine = mutableListOf<String>("curl", "-H", "'Authorization: Bearer ${gh_token}'", "-H", "'Accept: application/vnd.github.raw'", "--remote-name", "-L", "https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py")

	commandLine(cmdLine)
}

When I run this task I get this output:

> Task :downloadScript FAILED
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0   123    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (22) The requested URL returned error: 404

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':downloadScript'.
> Process 'command 'curl'' finished with non-zero exit value 22

The most interesting part here, that running the same command in terminal (in IntelliJ itself or MacOS terminal) passes and the file is downloaded, while replacing ${gh_token} with the real token of course.
curl -H 'Authorization: Bearer token_string' -H 'Accept: application/vnd.github.raw' --remote-name -L https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py

Why does passing the string to commandline() using a variable fails the commandline interpolation causing it to fail?

答案1

得分: 0

For anyone reaching here, I've found the solution and it is very simple.

The '[header]' (apostrophes) around the Authorization and Accept headers mess up the interpolation of commandLine().

Once I removed them it all worked as expected.

This is the correct task:

tasks.register<Exec>("downloadScript") {

    val cmdLine = mutableListOf<String>("curl", "-H", "Authorization: Bearer ${gh_token}", "-H", "Accept: application/vnd.github.raw", "--remote-name", "-L", "https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py")

    commandLine(cmdLine)
}
英文:

For anyone reaching here, I've found the solution and it is very simple.

The '[header]' (apostrophes) around the Authorization and Accept headers mess up the interpolation of commandLine().

Once I removed them it all worked as expected.

This is the correct task:

tasks.register&lt;Exec&gt;(&quot;downloadScript&quot;) {

    val cmdLine = mutableListOf&lt;String&gt;(&quot;curl&quot;, &quot;-H&quot;, &quot;Authorization: Bearer ${gh_token}&quot;, &quot;-H&quot;, &quot;Accept: application/vnd.github.raw&quot;, &quot;--remote-name&quot;, &quot;-L&quot;, &quot;https://api.github.com/repos/my_org/my_repo/contents/scripts/script.py&quot;)

    commandLine(cmdLine)
}

huangapple
  • 本文由 发表于 2023年5月10日 18:45:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定