2

I am trying to send a custom host header using feign client, but the consumer application is not picking them. I am using spring boot feign and not openfeign.

Apart from Host, everyother header seems to be working as expected.

 ResponseEntity<JSONObject> list(@PathVariable("name") String name,
            @RequestParam("id") String id, @RequestParam("modelName") String modelName,
            @RequestHeader(value = "Authorization", required = true) String authorizationHeader,
            @RequestHeader(value = "Host", required = true) String hostName);

When i pass hostname as "xyz.com" the consumer application is still taking default host header.

In Consumer code when i read value of Host header from controller class, i do not get zyz.com. instead it picks from dns name.

VIJ
  • 1,516
  • 1
  • 18
  • 34

2 Answers2

1

Same problem happened for me - the @Param and @Headers didn't work for the "Host" header. Two things that I changed solved the issue - use OkHttpClient, and add a requestInterceptor when building the client:

client = Feign.builder()
    .client(new OkHttpClient())
    .requestInterceptor(request -> request.header("Host", hostHeaderValue))
    .target(MyClient.class, url);
danovia
  • 39
  • 6
  • 1
    Thank you, besides did you try adding the below command in your vm arguments. I faced the same issue for restTemplate as well for some strange reason and this below helped me out. Never got time to try it for feign -Dsun.net.http.allowRestrictedHeaders=true – VIJ Jun 18 '21 at 06:53
0

The solution is included in the springframework. Add in the your property file:

feign.okhttp.enabled = true

And add the dependency of okhttp3, example in pom

<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
</dependency>

You can find more informations here

Flatch
  • 36
  • 1
  • 4