Skip to content

Passing the http.DefaultClient to a New client can lead to breakdowns #3028

Description

@aoterolorenzo

! Not sure if I'm getting into a bad practice of this should be an expected behaviour

Instantiating a client with the Default http Golang client (client := github.NewClient(http.DefaultClient) leads to the perversion of the http.DefaultClient content, which when used over the rest of the code, starts breaking when for example using a Bearer token as Authorization: It will return 201, appearing the header not being included in the response.

func NewMyController() *MyController {
	return &MyController{
		client:    http.DefaultClient,
	        baseURL: "myBaseUrl",
	}
}

If after making any Github calls with a client := github.NewClient(http.DefaultClient), we run this code

req, _ := http.NewRequest(http.MethodGet, "myUrl", nil)

req.Header.Set("Authorization", "Bearer myToken")
resp, err := http.DefaultClient.Do(req)

fmt.Println(resp.StatusCode)

The http.DefaultClient will seem perverted since it will appear the Authorization header is not being correctly sent, as the retrieved StatusCode will be 201.

Extrainfo: Using go 1.21.4

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions