Home > Enterprise >  Apache Camel headers don't work in http query
Apache Camel headers don't work in http query

Time:11-17

I am trying to call an external API with an access token parameter in query, by taking it from header, however for some reason it doesn't work, API returns error Access token not configured.

But when I'm calling log on the same string, access token from header appears to be in place.

Code from configure method in my Route:

from("direct:user_get")
        .log("https://api.vk.com/method/users.get?access_token=${header.access_token}&v=5.131")
        .to("https://api.vk.com/method/users.get?access_token=${header.access_token}&v=5.131");

Call of FluentProducerTemplate (Actual token replaced with "token_example"):

String userGetResponse = producerTemplate
        .to("direct:user_get")
        .withHeader("access_token", "token_example")
        .request(String.class);

When I pass token right into the .to() call itself, everything works fine, API returns valid response.

CodePudding user response:

You need to use toD because it is a dynamic endpoint. So what are you sending is this string ${header.access_token} and not the the header Token.

.toD("https://api.vk.com/method/users.get?access_token=${header.access_token}&v=5.131");

Normnal to doesn't transform the uri only toD make this. Check the Docs

The to is used for sending messages to a static endpoint. In other words to sends message only to the same endpoint.

The toD is used for sending message to a dynamic endpoint. The dynamic endpoint is evaluated on-demand by an Expression. By default, the Simple expression is used to compute the dynamic endpoint URI.

  • Related