how to get and set parameters in Camel HTTP Proxy

14,683

Try following route:

from("servlet://abc")  
    .process(new Processor(){
        @Override
        public void process(Exchange exchange) throws Exception {
            // Camel will populate all request.parameter and request.headers, 
            // no need for placeholders in the "from" endpoint
            String id = exchange.getIn().getHeader("id", String.class);
            String name = exchange.getIn().getHeader("name", String.class);           

            // This URI will override http://dummyhost
            exchange.getIn().setHeader(Exchange.HTTP_URI, "http://example.com");

            // Add input path. This will override the original input path.
            // If you need to keep the original input path, then add the id to the 
            // URI above instead
            exchange.getIn().setHeader(Exchange.HTTP_PATH, id);

            // Add query parameter such as "?name=xxx"
            exchange.getIn().setHeader(Exchange.HTTP_QUERY, "name="+name);     
    }
    .to("http://dummyhost")

If you request is http://localhost:8080/hello/world?id=111&name=moon, then it should be forwarded to http://example.com/111?name=moon.

Share:
14,683
bks4line
Author by

bks4line

I like logics and coding! more working towards Artificial Intelligence.

Updated on June 05, 2022

Comments

  • bks4line
    bks4line almost 2 years

    lets say for an example i have a code:

           from(servlet://abc?id={id}&name={name}).process(new Processor(){
         @Override
            public void process(Exchange arg0) throws Exception {
                id = arg0.getIn().getHeader("id", String.class);
                id_type = arg0.getIn().getHeader("name",String.class);
    
                System.out.println(id);
                System.out.println(name);
                String url = "//example.com/"+id+"?name="+name;
                System.out.println(url);
    
                /*Thread.sleep(10000);*/
    
            }.setHeader(Exchange.HTTP).to("http:"+url+"&bridgeEndpoint=true&throwExceptionOnFailure=false)"
    

    I dont see my url there. its showing null value. how to solve this problem? I used to set this string in Exchange header but its giving me java.lang.IllegalArgumentException:

  • Peter Keller
    Peter Keller about 10 years
    @bks4line From the Camel's doc: "If the option is true , HttpProducer will ignore the Exchange.HTTP_URI header, and use the endpoint's URI for request.". As we use Exchange.HTTP_URI, the option bridgeEndpoint is not needed. To use a custom header parameter in the URI instead, didn't work in my tests.
  • bks4line
    bks4line about 10 years
    Thanks Peter! Thanks for helping me every single time! :)
  • Peter Keller
    Peter Keller about 10 years
    If the solution worked for you, then don't hesitate to accept the answer... Thanks.