Camel: Access incoming headers

In HTTP requests (and other requests or messages) you usually get a payload and a bunch of headers. In Camel you can find the payload in the “body” variable like this:

public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
           from("timer:mytimer?repeatCount=1")
                  .to("https://www.google.com")
                  .log("${body}");
    }
}

So, how do you access the headers? There is another variable for this called “in” and it can be used like this:

public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
           from("timer:mytimer?repeatCount=1")
                  .to("https://www.google.com")
                  .log("${in.headers}") // Log all incoming  headers
                  .log("${in.headers.Content-Type}"); // Log a specific header
    }
}

Tested on Ubuntu 20.04.4 LTS, Apache Camel 3.20, Minikube v1.29.0 (Camel-K) and Java 1.8.0_352

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.