Monday, February 5, 2024

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

Issue

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?


Solution

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)
}


Answered By - erutan
Answer Checked By - David Goodson (WPSolving Volunteer)