You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by sim085 <si...@hotmail.com> on 2016/09/21 18:59:03 UTC

Why is the set body not being returned?

Hello, I am trying some Camel with Jetty examples. The problem I have is that
after I do an http request to another site jetty no longer returns the set
body.

For example,

[code]
    	from("jetty:http://localhost:8080").setBody(constant("OK")); 
[/code]

Returns "OK" when from the browser I call the above set URL. However if the
following will not work.

[code]
    	from("jetty:http://localhost:8080")
    	.to("jetty:http://www.google.com" + 
    		"?bridgeEndpoint=true" + 
        	"&throwExceptionOnFailure=false")
    	.setBody(constant("OK")); 
[/code]

This returns the response from google (see attachment at end of message) and
not "OK" even though after the call to google I am setting the body to "OK".

What I find strange is this ...

The following code prints in the console that the body is "OK" but the
response returned to the browser is still the response got from google.

[code]
    	from("jetty:http://localhost:8080")
    	.to("jetty:http://www.google.com" + 
    		"?bridgeEndpoint=true" + 
        	"&throwExceptionOnFailure=false")
    	.setBody(constant("OK"))
    	.log("THE BODY IS ${body}"); // This line prints "OK" in the console.
[/code]

After looking through the Jetty Camel documentation I saw an example which
was overriding the body and that one works.

[code]
    	from("jetty:http://localhost:8080")
    	.to("jetty:http://www.google.com" + 
    		"?bridgeEndpoint=true" + 
        	"&throwExceptionOnFailure=false")
    	.process(new Processor(){
    		public void process(Exchange e) throws Exception {
    			e.getOut().setBody("HELLO");
    		}
    	})
    	// .setBody(constant("OK"))	// LINE 1
    	.log("THE BODY IS ${body}");
[/code]

The above without LINE 1 remarked returns "HELLO" in both browser and
console and with LINE 1 not remarked it returns "OK" in both browser and
console.

So why does setBody work when e.getOut().setBody(...) is used but not if
this is used without the processor?


<http://camel.465427.n5.nabble.com/file/n5787905/screenshot.png> 





--
View this message in context: http://camel.465427.n5.nabble.com/Why-is-the-set-body-not-being-returned-tp5787905.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: Why is the set body not being returned?

Posted by sim085 <si...@hotmail.com>.
Thanks Siano, you are right. The header has the following properties;

[code]
CamelHttpResponseCode = 302
location = http://www.google.com.mt/?gws_rd=cr&ei=
[/code]

I now understand; the correct body is being sent back to the browser but
without removing the headers the browser sees the 302 status and redirects
to the given location.







--
View this message in context: http://camel.465427.n5.nabble.com/Why-is-the-set-body-not-being-returned-tp5787905p5787938.html
Sent from the Camel - Users mailing list archive at Nabble.com.

RE: Why is the set body not being returned?

Posted by "Siano, Stephan" <st...@sap.com>.
Hi,

You probably should try to log the headers before you remove them. Maybe there is some redirect header in the response from google (and your browser follows that overwriting your OK response).

Best regards
Stephan

-----Original Message-----
From: sim085 [mailto:sim085@hotmail.com] 
Sent: Donnerstag, 22. September 2016 13:35
To: users@camel.apache.org
Subject: Re: Why is the set body not being returned?

Hello again. It seems that it is something in the headers. I have added
".removeHeaders("*")" to the chain and now jetty is correctly returning the
set message through the "transform" call. 

i.e. this works - 

[code]
    	from("jetty:http://localhost:8080")
    	.transform(constant(""))
    	.setHeader(Exchange.HTTP_METHOD, constant("GET"))
		.to("jetty:http://www.google.com" + 
    			"?bridgeEndpoint=true" + 
    			"&throwExceptionOnFailure=false")
		.transform(constant("OK"))
    	.log("THE BODY IS ${body}")
    	.removeHeaders("*");
[/code]

I saw the code of the "transform" method and I saw how this seems to make a
copy of the old message and then update the body. This is why I tried to
clear the headers.

So is this normal?




--
View this message in context: http://camel.465427.n5.nabble.com/Why-is-the-set-body-not-being-returned-tp5787905p5787931.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Why is the set body not being returned?

Posted by sim085 <si...@hotmail.com>.
Hello again. It seems that it is something in the headers. I have added
".removeHeaders("*")" to the chain and now jetty is correctly returning the
set message through the "transform" call. 

i.e. this works - 

[code]
    	from("jetty:http://localhost:8080")
    	.transform(constant(""))
    	.setHeader(Exchange.HTTP_METHOD, constant("GET"))
		.to("jetty:http://www.google.com" + 
    			"?bridgeEndpoint=true" + 
    			"&throwExceptionOnFailure=false")
		.transform(constant("OK"))
    	.log("THE BODY IS ${body}")
    	.removeHeaders("*");
[/code]

I saw the code of the "transform" method and I saw how this seems to make a
copy of the old message and then update the body. This is why I tried to
clear the headers.

So is this normal?




--
View this message in context: http://camel.465427.n5.nabble.com/Why-is-the-set-body-not-being-returned-tp5787905p5787931.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Why is the set body not being returned?

Posted by souciance <so...@gmail.com>.
I would think it is because the setbody is setting the body in the IN
exchange and the response you get is from the OUT exchange due to the
request/reply nature of your operation. It works similarly with RabbitMQ
where you have to set exchangePattern to InOut to get request/reply. If you
log the actual headers you will see the exchange values so you can compare.

On Wed, Sep 21, 2016 at 8:59 PM, sim085 [via Camel] <
ml-node+s465427n5787905h18@n5.nabble.com> wrote:

> Hello, I am trying some Camel with Jetty examples. The problem I have is
> that after I do an http request to another site jetty no longer returns the
> set body.
>
> For example,
>
> [code]
>     from("jetty:http://localhost:8080").setBody(constant("OK"));
> [/code]
>
> Returns "OK" when from the browser I call the above set URL. However if
> the following will not work.
>
> [code]
>     from("jetty:http://localhost:8080")
>     .to("jetty:http://www.google.com" +
>     "?bridgeEndpoint=true" +
>         "&throwExceptionOnFailure=false")
>     .setBody(constant("OK"));
> [/code]
>
> This returns the response from google (see attachment at end of message)
> and not "OK" even though after the call to google I am setting the body to
> "OK".
>
> What I find strange is this ...
>
> The following code prints in the console that the body is "OK" but the
> response returned to the browser is still the response got from google.
>
> [code]
>     from("jetty:http://localhost:8080")
>     .to("jetty:http://www.google.com" +
>     "?bridgeEndpoint=true" +
>         "&throwExceptionOnFailure=false")
>     .setBody(constant("OK"))
>     .log("THE BODY IS ${body}"); // This line prints "OK" in the console.
> [/code]
>
> After looking through the Jetty Camel documentation I saw an example which
> was overriding the body and that one works.
>
> [code]
>     from("jetty:http://localhost:8080")
>     .to("jetty:http://www.google.com" +
>     "?bridgeEndpoint=true" +
>         "&throwExceptionOnFailure=false")
>     .process(new Processor(){
>     public void process(Exchange e) throws Exception {
>     e.getOut().setBody("HELLO");
>     }
>     })
>     // .setBody(constant("OK")) // LINE 1
>     .log("THE BODY IS ${body}");
> [/code]
>
> The above without LINE 1 remarked returns "HELLO" in both browser and
> console and with LINE 1 not remarked it returns "OK" in both browser and
> console.
>
> So why does setBody work when e.getOut().setBody(...) is used but not if
> this is used without the processor?
>
>
>
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://camel.465427.n5.nabble.com/Why-is-the-set-body-not-
> being-returned-tp5787905.html
> To start a new topic under Camel - Users, email
> ml-node+s465427n465428h31@n5.nabble.com
> To unsubscribe from Camel - Users, click here
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=465428&code=c291Y2lhbmNlLmVxZGFtLnJhc2h0aUBnbWFpbC5jb218NDY1NDI4fDE1MzI5MTE2NTY=>
> .
> NAML
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://camel.465427.n5.nabble.com/Why-is-the-set-body-not-being-returned-tp5787905p5787907.html
Sent from the Camel - Users mailing list archive at Nabble.com.