zip file is downloaded from github URL through curl command, but same command fails to download file from golang code

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

zip file is downloaded from github URL through curl command, but same command fails to download file from golang code

问题

我正在尝试通过使用curl命令从私有仓库的github URL下载一个zip文件,并通过传递身份验证令牌来进行身份验证,命令如下:

curl -H 'Authorization: token <个人访问令牌>' -H 'Accept: application/vnd.github.v3.raw' -J -L -O https://api.github.com/repos/<所有者>/<仓库名称>/contents/abc.zip

这个命令可以正确下载zip文件abc.zip,并且可以解压缩,其中包含了所有预期的文件。

但是,当从golang代码执行相同的命令时,它会下载一个以JSON格式的文件,并显示错误信息:

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

Golang代码片段:

Cmd := exec.Command("curl", "-H", "'Authorization: token <Token>'", "-H", "'Accept: application/vnd.github.v3.raw'", "-O", "-L", "https://api.github.com/repos/<所有者>/<仓库名称>/contents/abc.zip")
Cmd.Start()

我期望下载zip文件,但是它下载了一个类型为JSON数据的文件,其中包含了"Not Found"的错误消息。

我在这里可能做错了什么?

英文:

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 &#39;Authorization: token &lt;PERSONAL ACCESS TOKEN HERE&gt;’ -H &#39;Accept: application/vnd.github.v3.raw&#39; -J -L -O https://api.github.com/repos/&lt;owner&gt;/&lt;repo-name&gt;/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(&quot;curl&quot;, &quot;-H&quot;, &quot;&#39;Authorization: token &lt;Token&gt;&#39;&quot;, &quot;-H&quot;, &quot;&#39;Accept: application/vnd.github.v3.raw&#39;&quot;, &quot;-O&quot;, &quot;-L&quot;, &quot;https://api.github.com/repos/&lt;owner&gt;/&lt;repo-name&gt;/contents/abc.zip&quot;)
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?

答案1

得分: 3

在Shell中使用的curl命令内部的引号仅供Shell解释使用:为了提取命令和命令的参数,Shell通过空格分隔命令行。这意味着如果参数包含空格,需要对其进行引用,以防止Shell将其拆分为多个参数。但是,在Shell中执行命令时不使用这些引号。

由于您直接从Go中调用命令并将其拆分为参数,因此不需要进行进一步的Shell引号处理。您当前所做的是将带引号的参数'Authorization...'传递给curl,而不是不带引号的Authorization...

英文:

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 &#39;Authorization...&#39; (w/ quotes) to curl, instead of Authorization... (w/o quotes).

huangapple
  • 本文由 发表于 2023年6月7日 19:16:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76422736.html
匿名

发表评论

匿名网友

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

确定