英文:
curl command return 404 when using os/exec
问题
我尝试使用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上,但不知道如何修复它。
英文:
I try to get file from private gitlab
repository using os/exec
with curl
and get 404
response status:
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)
but when I try to use the same command from zsh
, I get right response:
% /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/
I think the problem in the url
, but don't understand how to fix one.
答案1
得分: 1
?
和=
不需要加引号:
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通配符。
英文:
?
and =
must not be quoted:
cmd := exec.Command(
`curl`,
`-H`, `PRIVATE-TOKEN:token`,
`https://gitlab.some.com/api/v4/projects/23/repository/files/.gitignore/raw?ref=master`,
)
exec.Command
does not spawn a shell, therefore shell globs do not need escaping.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论