Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set Request Headers Using a Feign Client?

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?

like image 566
Abhijit Sarkar Avatar asked May 18 '16 03:05

Abhijit Sarkar


1 Answers

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

like image 156
RobP Avatar answered Sep 17 '22 02:09

RobP