The solution is to use @RequestHeader annotation instead of feign specific annotations
@FeignClient(name="Simple-Gateway")
interface GatewayClient {
@RequestMapping(method = RequestMethod.GET, value = "/gateway/test")
String getSessionId(@RequestHeader("X-Auth-Token") String token);
}
Answer from Hasnain on Stack OverflowThe solution is to use @RequestHeader annotation instead of feign specific annotations
@FeignClient(name="Simple-Gateway")
interface GatewayClient {
@RequestMapping(method = RequestMethod.GET, value = "/gateway/test")
String getSessionId(@RequestHeader("X-Auth-Token") String token);
}
The @RequestHeader did not work for me. What did work was:
@Headers("X-Auth-Token: {access_token}")
@RequestLine("GET /orders/{id}")
Order get(@Param("id") String id, @Param("access_token") String accessToken);
You simple pass your header as an argument and you declare your method in your interface accordingly. Please find snippet of code below.
And when you run your Feign client with logging level full (feign.client.config.default.loggerLevel=full), you'll see the authorization header (being an OAuth Bearer access token in my case).
Hopefully this helps.
All the best, Wim
@FeignClient(name="mde", url="${MDE.campaignServiceEndpoint}")
public interface CampaignClientService {
@RequestMapping(method = RequestMethod.GET, value = "/useCases/search/findByName?name={name}")
@Cacheable("placementUseCase")
PlacementUseCase findUseCaseByName(@RequestHeader(value = "Authorization", required = true) String authorizationHeader, @PathVariable("name") String name);
If you are using Feign client directly without spring
then you can pass Header params something like this, in Feign Interface
@Headers("Content-Type: application/json")
@RequestLine("PATCH infusionsoft/contacts/{contactId}?format={format}")
InfusionsoftFullContact updateContact(@Param("contactId") Long contactId,
@Param("format") String format,
InfusionsoftFullContact contact,
@HeaderMap Map<String, Object> headerMap);
Where HeaderParams can be as below
Map<String, Object> headerMap = new HashMap<>();
headerMap.put("application", "FDX");
headerMap.put("Authorization", "Basic aW5mdXNpb25zb2Z0OnBhc3N3b3Jk");
headerMap.put("platform",user.getPlatform());
headerMap.put("platformKey", user.getPlatformKey());
headerMap.put("email",user.getEmail());
headerMap.put("Content-Type", MediaType.APPLICATION_JSON);
You can create an interceptor that'd inject headers in all requests as below:
@Bean
public RequestInterceptor requestInterceptor() {
return requestTemplate -> {
requestTemplate.header("user", username);
requestTemplate.header("password", password);
requestTemplate.header("Accept", ContentType.APPLICATION_JSON.getMimeType());
};
}
Also it provides a way to set interceptor using property files as below:
feign:
client:
config:
default:
requestInterceptors:
com.baeldung.cloud.openfeign.JSONPlaceHolderInterceptor
we can create the configuration with default as the client name to configure all @FeignClient objects, or we can declare the feign client name for a configuration
Reference: https://www.baeldung.com/spring-cloud-openfeign
EDIT: Another way is to set headers in yml as below:
feign:
client:
config:
default:
defaultRequestHeaders:
Authorization:
- Basic dXNlcjpwYXNzd29yZA==
SomeOtherHeader:
- Value1
- Value2
we have used below for kotlin to make sure we add proper headers to all the request sent :
Feign client with configuration setup
@FeignClient(name = "YourClient", url = "\${base-url}", configuration = [FeignHeaderConfiguration::class])
interface YourClient
Header configuration
@Configuration
class FeignHeaderConfiguration {
@Bean
fun clientHeaderInterceptor(): ClientHeaderInterceptor {
return ClientHeaderInterceptor()
}
}
and interceptor where we added the header
class ClientHeaderInterceptor : RequestInterceptor {
override fun apply(requestTemplate: RequestTemplate) {
requestTemplate.header("Accept", MediaType.APPLICATION_JSON_VALUE)
}
}