How to Set Request Headers Using a Feign Client?

Abhijit Sarkar picture Abhijit Sarkar · May 18, 2016 · Viewed 17.8k times · Source

We are developing a suite of Microservices using Spring Cloud framework and one of the the things that we need to do is to set request headers. I know I can pass a parameter @RequestHeader to a Feign method but the value needs to come from another bean. I don't know if SPEL can be used for a Feign param value. I was thinking that this is a common enough use case for most clients so there'd be some examples, but so far I've not found any. Of course I can dig through the Spring course code and try to override the default Feign configuration but it kinda defeats the purpose of a declarative client if I've to write a lot of code to achieve this. Any thoughts?

Answer

RobP picture RobP · May 19, 2016

I have done this before using a RequestInterceptor as follows:

@Component
public class MyRequestInterceptor implements RequestInterceptor {
  @Override
  public void apply(RequestTemplate template) {
    template.headers(getHeadersFromWherever());
  }
}

You can find some more useful information here:

https://github.com/Netflix/feign#user-content-setting-headers-per-target