Forum Home » Fuse Distributions » Fuse Mediation Router

Thread: Can't convert HTTP Response with no Content-Type to String

 

Permlink Replies: 6 - Last Post: Apr 10, 2012 11:08 AM Last Post By: jamie3
jamie3

Posts: 131
Registered: 07/24/08
Can't convert HTTP Response with no Content-Type to String
Posted: Apr 4, 2012 10:32 AM
  Click to reply to this thread Reply
I have a case where I am doing an HTTP POST to a web server and the response is text but camel cannot convert the body to a string. Instead the conversion results in an empty string.

from("timer://foo")
.to("http://somehost/bar")
.convertBodyTo(String.class)
// this results in an empty string
.log("${body}");

I examined the response body and it is a CachedOutputStream$WrappedInputStream. If I introspect through the body I can see the actual text that the server is responding with.

I turned on debugging for the camel http component and it is saying that the Content-Type isn't found in the http response. Would this contribute to the problem?

My only work around is to write my own type converter, however I was hoping camel's built in type converters could handle this.

Or I was hoping to somehow force the http endpoint to read the response as text.
njiang

Posts: 572
Registered: 09/17/07
Re: Can't convert HTTP Response with no Content-Type to String
Posted: Apr 5, 2012 1:35 AM   in response to: jamie3 in response to: jamie3
  Click to reply to this thread Reply
I went through the camel-http trunk code, the response content-type is optional.
I also did some test with the camel HttpProducer, I can get the response if there is no Content-Type.

So I just want to ask you some question:

What version of camel are you using?
Did you add any custom type converter yourself?
Does your web server use gzip to compress the data?

Willem
jamie3

Posts: 131
Registered: 07/24/08
Re: Can't convert HTTP Response with no Content-Type to String
Posted: Apr 5, 2012 12:27 PM   in response to: njiang in response to: njiang
  Click to reply to this thread Reply
Here's what I've had to do to be able to perform my conversion.

from("timer://foo")
.to("http://.....")
.process(new Processor() {
// for some reason the cached data output stream has already read the inputstream
// so we need to reset the stream position in order for the converters to grab the data
public void process(Exchange exg) {
InputStream in = exg.getIn().getBody(InputStream.class);
in.reset();
}
})
.convertBodyTo(String.class)
.log("${body}") // no longer outputs an empty string
jamie3

Posts: 131
Registered: 07/24/08
Re: Can't convert HTTP Response with no Content-Type to String
Posted: Apr 5, 2012 12:28 PM   in response to: jamie3 in response to: jamie3
  Click to reply to this thread Reply
im using camel.2.8.0-fuse-01-11
no gzip
jamie3

Posts: 131
Registered: 07/24/08
Re: Can't convert HTTP Response with no Content-Type to String
Posted: Apr 5, 2012 1:49 PM   in response to: jamie3 in response to: jamie3
  Click to reply to this thread Reply
I think i found the problem. In my real code I spit out the response from the HTTP GET. The logging EIP seems to read the inputstream which moves the position to the end of the buffer.

// this fails
from("timer://foo")
.log("${body}")
.convertBodyTo(String.class)
.log("${body}") // empty string

If i convert the response to a string first, then the output in the log is valid

// this works
from("timer://foo")
.convertBodyTo(String.class)
.log("${body}") // non empty string
njiang

Posts: 572
Registered: 09/17/07
Re: Can't convert HTTP Response with no Content-Type to String
Posted: Apr 10, 2012 6:43 AM   in response to: jamie3 in response to: jamie3
  Click to reply to this thread Reply
As the message body is a stream, you should enable the stream cache to let camel consume it more than one.

Willem
jamie3

Posts: 131
Registered: 07/24/08
Re: Can't convert HTTP Response with no Content-Type to String
Posted: Apr 10, 2012 11:08 AM   in response to: njiang in response to: njiang
  Click to reply to this thread Reply
Thanks! I'll keep that in mind.

The HTTP response payloads are very small so converting to String does the trick.