arora_suman
arora_suman

Reputation: 21

Authorization Bearer token in Apache Camel

I want to use Apache Camel to call a rest webservice which takes a bearer token as authorization. I am getting 401 unauthorized access if I use the below code--

@Component
public class LoadFileRouteBuilder extends RouteBuilder{
    
    @Override
    public void configure() throws Exception {

        from("timer:mytimer?repeatCount=1")
            .log("Scheduled job!")
            .to("direct:httpRoute");
            
        from("direct:httpRoute")
            .log("Http Route started")
            .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
            .setHeader("Authorization").simple("Bearer "+ myToken)
            .to("https://my-rest-ws-url")
            .log("Response : ${body}");
    }   
}

I have also tried using process method to set the authorization token exchange.getIn().setHeader("Authorization", "Bearer "+myToken); didn't work either.

Is it the correct way to send bearer token ?

Upvotes: 2

Views: 3453

Answers (1)

WesternGun
WesternGun

Reputation: 12787

I use:

Message message = exchange.getIn();
message.setHeader("Authorization", "Bearer " + token);

This works for me. I can see token in the Exchange properties. Apache Camel 2.7.5.

Slightly different though, is that I do it in a .bean(SomeBean.class) and then send to toD() with dynamic URL.

Upvotes: 1

Related Questions