Home > Enterprise >  curl command return 404 when using os/exec
curl command return 404 when using os/exec

Time:01-02

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.

CodePudding user response:

? 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.

  • Related