Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom header to request #679

Open
ainsleyclark opened this issue Oct 30, 2023 · 0 comments
Open

Add custom header to request #679

ainsleyclark opened this issue Oct 30, 2023 · 0 comments

Comments

@ainsleyclark
Copy link

Is there an idiomatic way to add a header to any given request? It seems below doesn't quite work.
No header is attached to the ouath call:

type CustomTransport struct {
	Base http.RoundTripper
}

func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	req.Header.Add("My-Header", "whatever")
	fmt.Println(req.Header)
	return t.Base.RoundTrip(req)
}

func Temp() error {
	credentials, err := google.CredentialsFromJSON(context.Background(), []byte("my-creds"), sheets.SpreadsheetsReadonlyScope)
	if err != nil {
		return err
	}

	client := &http.Client{
		Transport: &CustomTransport{
			Base: oauth2.NewClient(context.Background(), credentials.TokenSource).Transport,
		},
	}

	sheetClient, err := sheets.NewService(context.Background(), option.WithHTTPClient(client))
	if err != nil {
		return err
	}

	do, err := sheetClient.Spreadsheets.Get("ID").Do()
	if err != nil {
		return err
	}

	fmt.Println(do)
	return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant