How do I set the Content-Type on a camel-restlet producer request?

Rodrigo Del C. Andrade picture Rodrigo Del C. Andrade · Mar 20, 2015 · Viewed 9.5k times · Source

I need to consume simple Rest service, but their implementation breaks if my request goes out with Content-type: application/x-www-form-urlencoded. I need to set it as "application/json"or be faced with a status 415.

I am using the restlet producer component because it is already used throughout and so far it had hit the sweet spot between functionality and simplicity. So far.

Anyway, trying to set the header in my route seems to have zero effect and the content-type of my request remains as application/x-www-form-urlencoded. Here is my test code:

    from("direct:getImg")
            .setHeader(RestletConstants.RESTLET_LOGIN, simple("admin"))
            .setHeader(RestletConstants.RESTLET_PASSWORD, simple("admin"))
            .setHeader(Exchange.CONTENT_TYPE, simple("application/json"))
            .to("restlet:http://requestb.in/12sowlx1?restletMethod=get&throwExceptionOnFailure=false")

I obviously am missing something, but I cant find any example. Can anyone point the right way to do it?

Thanks!

Answer

Zabin picture Zabin · Mar 26, 2015

You should call a processor before you call your restlet and set the content type in the exchange. Something like this:

from("direct:getImg").process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_XML);
        }
    }).to("restlet:http://requestb.in/12sowlx1?restletMethod=get&throwExceptionOnFailure=false");

I have tested it and it works. Let me know the result.