5

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!

mpromonet
  • 11,326
  • 43
  • 62
  • 91
  • setHeader(Exchange.CONTENT_TYPE, constant("application/json")) would work . Let me know your result. – Naveen Raj Mar 22 '15 at 13:25
  • @NaveenRaj - same problem. No effect. You can see the details of my outgoing request here: http://requestb.in/ugszbsug?inspect (the one in my original quest expired) – Rodrigo Del C. Andrade Mar 22 '15 at 15:55

1 Answers1

4

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.

Zabin
  • 64
  • 1
  • 1
    I guess the header has to be set in outbond message i.e we need to use exchange.getOut().setHeader(.......). Editing the same in comments. – Vivek Dhayalan Jan 11 '16 at 13:42