Himalay Majumdar
Himalay Majumdar

Reputation: 3973

Appending X to Custom Request Headers in Spring-Integration

I found that when I want to make a REST call using Spring-Integration, it automatically appends 'x' in case its a custom header.

For example in Spring-integration while sending custom request headers such as API-KEY, the actual request header name in the API call becomes X-API-KEY and so it fails.

It seems like Spring is standardizing by enforcing the Custom request headers to start with X, is there a work around?

<int:channel id="requestChannel"/>
<int:channel id="httpHeaderEnricherChannel"/>
<int-http:outbound-gateway request-channel="requestChannel"
                                       url="http://localhost:9090/balance"
                                       http-method="GET"
                                       mapped-request-headers="Api-Key"
                                       expected-response-type="java.lang.String"/>

<int:header-enricher input-channel="httpHeaderEnricherChannel" 
                     output-channel="requestChannel">
    <int:header name="Api-Key" value="pass"/>
</int:header-enricher>

Upvotes: 2

Views: 2209

Answers (2)

Himalay Majumdar
Himalay Majumdar

Reputation: 3973

Thanks to @Artem for clarifying, and Gary's post here Spring Integration Http Outbound Gateway Header Mapper

I was able to solve the issue

<int:channel id="requestChannel"/>
<int:gateway id="requestGateway" 
             service-interface="org.springframework.integration.samples.http.RequestGateway"
             default-request-channel="requestChannel">
            <int:default-header name="Api-Key" value="pass" />
</int:gateway>

<int-http:outbound-gateway request-channel="requestChannel"
                           header-mapper="headerMapper"
                           url="http://localhost:9090/balance"
                           http-method="GET"
                           expected-response-type="java.lang.String"/>

<beans:bean id="headerBean"
            class="org.springframework.integration.samples.http.HeaderBean" />

<bean id="headerMapper"
    class="org.springframework.integration.http.support.DefaultHttpHeaderMapper">
    <property name="inboundHeaderNames" value="*" />
    <property name="outboundHeaderNames" value="HTTP_REQUEST_HEADERS, Api-Key" />
    <property name="userDefinedHeaderPrefix" value="" />
</bean>

Upvotes: 0

Artem Bilan
Artem Bilan

Reputation: 121550

You should declare DefaultHttpHeaderMapper.outboundMapper() bean with the setUserDefinedHeaderPrefix(null) and including that your custom Api-Key header mapping. After that you should replace mapped-request-headers attribute with the header-mapper reference.

We have revised the feature and decided to remove "X-" default prefix in the next version.

For more info please, see here Custom HTTP headers : naming conventions and here https://jira.spring.io/browse/INT-3903.

Upvotes: 3

Related Questions