Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 934
Rodrigo Del C. Andrade Avatar asked Sep 29 '22 12:09

Rodrigo Del C. Andrade


1 Answers

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.

like image 145
Zabin Avatar answered Oct 31 '22 19:10

Zabin