Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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(
        `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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading