curl command return 404 when using os/exec

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(
        `-H`, `PRIVATE-TOKEN:token`,

    var out bytes.Buffer
    cmd.Stdout = &out

    err := cmd.Run()
    if err != nil {

=== RUN   Test_curl
    t_test.go:166: /usr/bin/curl -H PRIVATE-TOKEN:token\?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\?ref\=master

I think the problem in the url, but don’t understand how to fix one.

>Solution :

? and = must not be quoted:

cmd := exec.Command(
    `-H`, `PRIVATE-TOKEN:token`,

exec.Command does not spawn a shell, therefore shell globs do not need escaping.

Leave a Reply