简体   繁体   中英

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM