0

I have a really simple route that GETs an URL and prints the content using Camel HTTP4 component:

from("timer://foo?fixedRate=true&delay=0&period=10000")
    .to("http4://www.google.com")
    .process(e -> System.out.println("Out body: " + e.getOut().getBody()));

Note that I'm using out.body because, as stated in Camel documentation:

Camel will store the HTTP response from the external server on the OUT body. All headers from the IN message will be copied to the OUT message, so headers are preserved during routing.

But I get null values from OUT (both body and headers). Everything is being filled only in the IN message.

Am I missing anything or is it a bug?

jfneis
  • 2,139
  • 18
  • 31

2 Answers2

0

You are getting the Out body from the processor without setting it up first. That’s why you get null. To make this work you first need explicitly copy the incoming message, headers and attachments to Out Body and then print it. Or more easily take the In message as you mentioned.

Next part is from “Camel in Action” book which is a great book and I think it is very helpful.

in practice there’s a common pitfall when using getOut: the incoming message headers and attachments will be lost. This is often not what you want, so you must copy the headers and attachments from the incoming message to the outgoing message, which can be tedious.

Themis Pyrgiotis
  • 876
  • 5
  • 15
  • I understand that if I'm processing an input message the OUT will be empty by default. But HTTP4 component explicit states that the IN message will be copied to OUT message (body/headers). Shouldn't OUT be available after sending a message using HTTP4? If not, the documentation is misleading, do you agree? – jfneis Aug 18 '17 at 14:10
  • 1
    In Camel a route consists of nodes. Each node takes the Exchange. Exchange has an IN and OUT message. So in your case, node with http4 component took the Exchange, called google.com and wrote body and headers to OUT message. Next, node with your proccesor took the Exchange. Now IN message has the response from the previous node(http4), but you are printing OUT which is empty! So IN and OUT message are per node not per route! Hope it was helpful! – Themis Pyrgiotis Aug 18 '17 at 15:08
  • Oh man, that's the real answer! Now it's clear, you're right. Please copy your last comment as the answer of the question and I'll be glad to accept it! Thank you! – jfneis Aug 18 '17 at 20:10
0

In Camel a route consists of nodes. Each node takes the Exchange. Exchange has an IN and OUT message. So in your case, node with http4 component took the Exchange, called google.com and wrote body and headers to OUT message. Next, node with your proccesor took the Exchange. Now IN message has the response from the previous node(http4), but you are printing OUT which is empty! So IN and OUT message are per node not per route!

Themis Pyrgiotis
  • 876
  • 5
  • 15