title: How to add dynamic header values to feign-client through Feign interceptor from current request?
body: <p>I am new to Spring-cloud-openfeign. I am trying to build a micro-service based system. To make things look simpler, just take 3 of those services, which are Gateway, Service-A, Service-B. When I make a request from my frontend to Service-A through Gateway, the gateway will verify the JWT token in the request, and extract user information (userId) from the token and will put in the request as a header and will forward it to Service-A. Now Service-A will call service-B through feign-client. Now during this inter-service call through feign-client, I am trying to forward userId from the current request in Serice-A to outgoing request to service-B through a Feign RequestIterceptor. But I am not able to retrieve current request in the interceptor. I tried the solution in this <a href="https://stackoverflow.com/questions/49168320/feign-client-dynamic-authorization-header">stack_over_question</a>, but it doesn't seem to work. I think I face the same problem as of this <a href="https://github.com/spring-cloud/spring-cloud-netflix/issues/2549" rel="nofollow noreferrer">github_issue</a>. I could see some blogs and articles advising to use RequestContextListner or RequestContextFilter to get the current request out of the Dispatcher-Servlet, But I couldn't find out how to use or implement it. Following is the code I am currently using</p>
  <pre><code>@Component
  public class ProxyInterceptor implements RequestInterceptor {
          
          
          private final static Logger LOGGER = LoggerFactory.getLogger(ProxyInterceptor.class);
          
          @Override
          public void apply(RequestTemplate requestTemplate) {
                  
                  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                          .getRequest();
                  
                  String userId = request.getHeader(&quot;userId&quot;);
                  LOGGER.info(&quot;userId {}&quot;, userId);
                  
                  if (Optional.ofNullable(userId).isPresent()) {
                          requestTemplate.header(USER_ID_REQUEST_HEADER_VARIABLE, userId);
                  }
          }
  }
  </code></pre>
  <p>Dependecies</p>
  <pre><code>&lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-openfeign&lt;/artifactId&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;                     
              &lt;artifactId&gt;spring-cloud-starter-circuitbreaker-resilience4j&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  <blockquote>
  <p>&lt;spring-cloud.version&gt;Hoxton.SR8&lt;/spring-cloud.version&gt;</p>
  </blockquote>
  <p>This code throws a NullPointer exception as RequestContextHolder.getRequestAttributes() return null. Any idea how to solve this problem?</p>