From a4d603aefef181d6c04a318d3cb577b225a9de0d Mon Sep 17 00:00:00 2001 From: askuy Date: Fri, 10 May 2024 17:33:56 +0800 Subject: [PATCH] set http custom header --- client/ehttp/interceptor.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/client/ehttp/interceptor.go b/client/ehttp/interceptor.go index 63e5004a..6f2dc264 100644 --- a/client/ehttp/interceptor.go +++ b/client/ehttp/interceptor.go @@ -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)) } } @@ -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 @@ -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) {