404 when using the Go http client
<p>I want to programmatically authenticate against the Docker hub service. When I run my code, I get a 404 error. But when I pass the same URL into Curl, I get a 200. I'm not sure how to debug the problem.</p>
<p>I use the following code:</p>
<pre><code>import (
    &quot;bytes&quot;
    &quot;encoding/json&quot;
    &quot;fmt&quot;
    &quot;io/ioutil&quot;
    &quot;log&quot;
    &quot;net/http&quot;
    &quot;strings&quot;
    &quot;time&quot;
)

type Image struct {
    host, name, tag string
}

func authenticate(image Image) (token string) {
    var url = fmt.Sprintf(&quot;https://auth.docker.io/token?service=%s&amp;scope=repository:%s:pull&quot;, image.host, image.name)
    var req, e1 = http.NewRequest(&quot;get&quot;, url, nil)
    if e1 != nil {
        log.Fatal(e1)
    }
    fmt.Println(req)
    var client = &amp;http.Client{
        Timeout:       time.Second * 10,
        CheckRedirect: nil,
        Jar:           nil,
    }
    var res, e2 = client.Do(req)
    if e2 != nil {
        log.Fatal(e2)
    }
    var buf, e3 = ioutil.ReadAll(res.Body)
    if e3 != nil {
        log.Fatal(e3)
    }
    if res.StatusCode &gt; 299 {
        log.Fatal(res.StatusCode, &quot;: &quot;, bytes.NewBuffer(buf).String())
    }
    type AuthResponse struct {
        token string
    }
    var authres AuthResponse
    fmt.Println(bytes.NewBuffer(buf).String())
    var e4 = json.Unmarshal(buf, &amp;authres)
    if e4 != nil {
        log.Fatal(e4)
    }
    return authres.token
}
</code></pre>
<p>My print statements output:</p>
<blockquote>
<p>&amp;{registry.docker.io waisbrot/wait latest}</p>
<p>&amp;{get <a href="https://auth.docker.io/token?service=registry.docker.io&amp;scope=repository:waisbrot/wait:pull" rel="nofollow noreferrer">https://auth.docker.io/token?service=registry.docker.io&amp;scope=repository:waisbrot/wait:pull</a> HTTP/1.1 1 1 map[]  0 [] false auth.docker.io map[] map[]  map[]      }</p>
<p>2016/09/11 11:19:42 404: 404 page not found</p>
</blockquote>
<p>But the URL shown does not produce a 404 in Curl (or in a web browser), so I don't understand why Go is seeing an error.</p>