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

set http custom header #382

Merged
merged 1 commit into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions client/ehttp/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func logAccess(name string, config *Config, logger *elog.Component, req *resty.R
for _, key := range loggerKeys {
if value := req.Context().Value(key); value != nil {
fields = append(fields, elog.FieldCustomKeyValue(key, cast.ToString(value)))
req.SetHeader(key, cast.ToString(value))
}
}

Expand Down Expand Up @@ -140,6 +139,17 @@ func fixedInterceptor(name string, config *Config, logger *elog.Component, build
}

func logInterceptor(name string, config *Config, logger *elog.Component, builder resolver.Resolver) (resty.RequestMiddleware, resty.ResponseMiddleware, resty.ErrorHook) {
loggerKeys := transport.CustomContextKeys()
beforeFn := func(cli *resty.Client, req *resty.Request) error {
// 增加header
for _, key := range loggerKeys {
if value := req.Context().Value(key); value != nil {
req.SetHeader(key, cast.ToString(value))
}
}
return nil
}

afterFn := func(cli *resty.Client, response *resty.Response) error {
logAccess(name, config, logger, response.Request, response, nil)
return nil
Expand All @@ -151,7 +161,7 @@ func logInterceptor(name string, config *Config, logger *elog.Component, builder
logAccess(name, config, logger, req, nil, err)
}
}
return nil, afterFn, errorFn
return beforeFn, afterFn, errorFn
}

func metricInterceptor(name string, config *Config, logger *elog.Component, builder resolver.Resolver) (resty.RequestMiddleware, resty.ResponseMiddleware, resty.ErrorHook) {
Expand Down
Loading