jfneis
jfneis

Reputation: 2197

http4 not setting HTTP_RESPONSE properties

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?

Upvotes: 0

Views: 585

Answers (2)

Themis Pyrgiotis
Themis Pyrgiotis

Reputation: 896

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!

Upvotes: 0

Themis Pyrgiotis
Themis Pyrgiotis

Reputation: 896

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.

Upvotes: 0

Related Questions