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 example for api client that supports custom headers #1653

Closed
andydunstall opened this issue Oct 16, 2024 · 3 comments
Closed

Add example for api client that supports custom headers #1653

andydunstall opened this issue Oct 16, 2024 · 3 comments

Comments

@andydunstall
Copy link

We're using the client to query metrics in victoria metrics, which we only have a bearer token for authentication, meaning we need to add header Authorization: Bearer xxx (rather than adding a username/password to the URL)

Maybe I'm missing something, but that doesn't see possible with this client (other than adding a custom round tripper and injecting headers, though the Go docs say RoundTrip should not modify the request)

It would be great to extend api.Config to support custom headers, such as:

// Config defines configuration parameters for a new client.
type Config struct {
	// The address of the Prometheus to connect to.
	Address string

	// Headers contains custom HTTP request headers.
	Headers map[string]string

	// Client is used by the Client to drive HTTP requests. If not provided,
	// a new one based on the provided RoundTripper (or DefaultRoundTripper) will be used.
	Client *http.Client

	// RoundTripper is used by the Client to drive HTTP requests. If not
	// provided, DefaultRoundTripper will be used.
	RoundTripper http.RoundTripper
}

I looked through open issues and PRs and couldn't see anything relating to this, though I'd be surprised if we were the first to run into this issue so sorry if this is a duplicate

@dosubot dosubot bot added the enhancement label Oct 16, 2024
@bwplotka
Copy link
Member

This is fairly easy to do thanks to ability to specify custom httpClient/transport or roundtripper, e.g. you can use NewHeadersRoundTripper from a common module to do so, or write a quick roundtripper yourself.

Does this help? (:

I will keep this issue open to add an example for this.

@bwplotka bwplotka changed the title Support custom headers Add example for api client that supports custom headers Oct 18, 2024
@bwplotka
Copy link
Member

Fixed in #1657

@andydunstall
Copy link
Author

Great thanks for the help :)

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

No branches or pull requests

2 participants