1

I am trying to download a zip file from a github URL from a private repo by passing an authentication token using a curl command as follows:

curl -H 'Authorization: token <PERSONAL ACCESS TOKEN HERE>’ -H 'Accept: application/vnd.github.v3.raw' -J -L -O https://api.github.com/repos/<owner>/<repo-name>/contents/abc.zip

This command downloads the zip file abc.zip properly and can be unzipped and it has all expected files

But same command when executed from the golang code, it downloads a file in json format with error:

{ "message": "Not Found", "documentation_url": "https://docs.github.com/rest/reference/repos#get-repository-content" }

Golang code snippet:

Cmd := exec.Command("curl", "-H", "'Authorization: token <Token>'", "-H", "'Accept: application/vnd.github.v3.raw'", "-O", "-L", "https://api.github.com/repos/<owner>/<repo-name>/contents/abc.zip")
Cmd.Start()

I expected the zip file to be downloaded, but it downloaded a file of type JSON data with message "Not Found".

What could I be doing wrong here?

user2306856
  • 81
  • 15
  • 3
    remove the single quote from the header value. See [this answer](https://stackoverflow.com/questions/76322962/why-doesnt-the-file-appear-after-exec-commandiconv/76345484#76345484) that shows how to diagnose this kind of problems. – Zeke Lu Jun 07 '23 at 11:21
  • Thanks a lot @ZekeLu for the fix and the link. The solution worked! – user2306856 Jun 07 '23 at 11:27

1 Answers1

3

The quoting inside the curl command used on the shell is only for interpretation by the shell: In order to extract command and arguments of a command the shell separates the command line by white space. This means one needs to quote an argument if it contains white space in order to prevent splitting into multiple arguments by the shell. These quotes are not used when executing the command by the shell though.

Since you call the command directly from Go and already split the command into arguments, then no further shell-quoting should be done. What you currently do is to give the argument 'Authorization...' (w/ quotes) to curl, instead of Authorization... (w/o quotes).

Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172