我尝试使用 os/exec
和 curl
从私有 gitlab
存储库获取文件并获取 404
响应状态:
func test_curl(t *testing.t) { cmd := exec.command( `curl`, `-h`, `private-token:token`, `https://gitlab.some.com/api/v4/projects/23/repository/files/.gitignore/raw?ref=master`, ) t.log(cmd.string()) var out bytes.buffer cmd.stdout = &out err := cmd.run() if err != nil { t.fatal(err) } t.log(out.string()) }
=== run test_curl t_test.go:166: /usr/bin/curl -h private-token:token https://gitlab.some.com/api/v4/projects/23/repository/files/.gitignore/raw?ref=master t_test.go:175: {"error":"404 not found"} --- pass: test_curl (0.25s)
但是当我尝试使用来自 zsh
的相同命令时,我得到了正确的响应:
% /usr/bin/curl -h private-token:token https://gitlab.some.com/api/v4/projects/23/repository/files/.gitignore/raw?ref=master
.DS_Store .vs/ .vscode/ .idea/
我认为问题出在 url
上,但不明白如何修复。
?
和 =
不得加引号:
cmd := exec.Command( `curl`, `-H`, `PRIVATE-TOKEN:token`, `https://gitlab.some.com/api/v4/projects/23/repository/files/.gitignore/raw?ref=master`, )
exec.command
不会生成 shell,因此 shell glob 不需要转义。