1

I want to list my repositories on GitHub. Using curl, I am able to do this:

curl -u USR:TOKEN https://api.github.com/user/repos

When I translate this code to Go:

func FetchRepos(usr, token string) []Repository {
    req, reqErr := http.NewRequest("Get", "https://api.github.com/user/repos", nil)
    if reqErr != nil {
        panic(reqErr)
    }

    req.SetBasicAuth(usr, token)

    resp, respErr := http.DefaultClient.Do(req)
    if respErr != nil {
        panic(respErr)
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    // Decode json response
    var data []Repository
    json.Unmarshal(body, &data)

    return data
}

I get an error printing out the resp variable gives me a 403 Forbidden.

I don't know what I'm doing wrong in this instance.

I have tried adding req.Header.Add("User-Agent", "request") but this did not have any effect.

Jomy
  • 514
  • 6
  • 22
  • It seems a redirection error . Please check the answers from https://stackoverflow.com/questions/16673766/basic-http-auth-in-go – Osman Corluk Apr 19 '22 at 17:53
  • Thank you for the tip. The answer just underneath made me realize the "Get" had to be fully capitalized. – Jomy Apr 20 '22 at 09:55

0 Answers0