英文:
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<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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论