Feign: Headers not taken into account on Post + Body

Created on 24 Jul 2018  路  2Comments  路  Source: OpenFeign/feign

Hello,

I'm trying to call my Feign client and I need to set dynamically a header for my request. I defined my client as follows:

import java.util.Map;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "cbp", url = "http://localhost:8090")
@RequestMapping("/cbp-core")
public interface CBPRESTClientChp {

  @RequestMapping(method = RequestMethod.POST, value = "/in", consumes = MediaType.APPLICATION_JSON_VALUE)
  String sendMessage(@RequestHeader Map<String, String> headerMap, @RequestBody String message);
}

However, when I call it my custom header is not taken into account and my application fails:

import org.springframework.http.HttpHeaders;

public class MyClass{
  ...
  @Override
  public String sendMessage(String tenantId, String message) {
    Map<String, String> headerMap = new HashMap<>();
    headerMap.put(HttpHeaders.HOST, tenantId);

    return cbpRestClient.sendMessage(headerMap, message);
  }
}

I'm I doing this wrong ? there's another way to specify headers + body in a post request ?
Thanks in advance for your help !

question spring-cloud

Most helpful comment

I hate to open old posts, but actually you can set the Host header with FeignClient.
Feign is based upon java.net.HttpUrlConnection. Setting the System property sun.net.http.allowRestrictedHeaders to true will enable the Host header overriding with the @RequestHeaders annotation.

All 2 comments

For clarity, you are using the Spring Cloud based annotations and not the Feign annotations. This question is better suited for the Spring Cloud OpenFeign team. However, I'll see if I can help. From what I can see, you are using the @RequestHeaders appropriately. But, your example will not work. You cannot change the host by changing the HOST header using Feign. The Host is set in the FeignClient. If you need a dynamic host, you'll need to handle that another way.

I hate to open old posts, but actually you can set the Host header with FeignClient.
Feign is based upon java.net.HttpUrlConnection. Setting the System property sun.net.http.allowRestrictedHeaders to true will enable the Host header overriding with the @RequestHeaders annotation.

Was this page helpful?
0 / 5 - 0 ratings