Skip to content

Commit f018bab

Browse files
committed
allow header and set header in client
1 parent f42cf82 commit f018bab

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

http/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type client struct {
2727
httpClient *http.Client
2828
ua string
2929
apiPrefix string
30+
headers map[string]string
3031
fallback cmds.Executor
3132
}
3233

@@ -40,6 +41,16 @@ func ClientWithUserAgent(ua string) ClientOpt {
4041
}
4142
}
4243

44+
// ClientWithUserAgent specifies the HTTP user agent for the client.
45+
func ClientWithHeaders(key, value string) ClientOpt {
46+
return func(c *client) {
47+
if c.headers == nil {
48+
c.headers = map[string]string{}
49+
}
50+
c.headers[key] = value
51+
}
52+
}
53+
4354
// ClientWithHTTPClient specifies a custom http.Client. Defaults to
4455
// http.DefaultClient.
4556
func ClientWithHTTPClient(hc *http.Client) ClientOpt {
@@ -173,6 +184,10 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
173184
}
174185
httpReq.Header.Set(uaHeader, c.ua)
175186

187+
for key, val := range c.headers {
188+
httpReq.Header.Set(key, val)
189+
}
190+
176191
httpReq = httpReq.WithContext(req.Context)
177192
httpReq.Close = true
178193

http/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ func (cfg *ServerConfig) SetAllowCredentials(flag bool) {
8585
cfg.corsOpts.AllowCredentials = flag
8686
}
8787

88+
func (cfg *ServerConfig) SetAllowHeaders(headers ...string) {
89+
cfg.corsOptsRWMutex.Lock()
90+
defer cfg.corsOptsRWMutex.Unlock()
91+
cfg.corsOpts.AllowedHeaders = append(cfg.corsOpts.AllowedHeaders, headers...)
92+
}
93+
8894
// allowOrigin just stops the request if the origin is not allowed.
8995
// the CORS middleware apparently does not do this for us...
9096
func allowOrigin(r *http.Request, cfg *ServerConfig) bool {

0 commit comments

Comments
 (0)