You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by daveharris <da...@harris.org.nz> on 2011/07/28 04:43:57 UTC

camel-http: Multiple Request Paramaters

Hi,
I am writing a route which sends a HTTP GET request with a bunch of request
parameters.

Looking at the documentation, the way to do this is via:
from("direct:start")
  .setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short"))
  .to("http://oldhost");

This is fine, but I was hoping for a cleaner way of doing it  -where I don't
have to handle escaping and &s.
I've been talking to @Blair and he suggested passing in a map to
Exchange.HTTP_QUERY but that didn't work.

My route defintion turns into a somewhat ugly-looking:
from("...")
.setHeader(Exchange.HTTP_QUERY, 
  constant("app=").append(header(APPLICATION))
  .append("&from=").append(header(FROM))
  .append("&to=").append(header(TO))
  .append("&msg=").append(header(MESSAGE))
)
.to("http://localhost/service")

To produce a HTTP get request of
http://localhost:3000/service?app=test&from=a&to=b&msg=hi

Is this is the only/best way to do it?  Just checking that there isn't some
shortcut which I can use instead.

The above *works* perfectly well, just being pedantic...

Thanks,
Dave

--
View this message in context: http://camel.465427.n5.nabble.com/camel-http-Multiple-Request-Paramaters-tp4641050p4641050.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-http: Multiple Request Paramaters

Posted by Claus Ibsen <cl...@gmail.com>.
On Fri, Jul 29, 2011 at 8:11 AM, Willem Jiang <wi...@gmail.com> wrote:
> If the Map.toString() supports to generate a right query string, I think you
> don't need to change other part camel code.
>

Thats a bit unusual to have a custom Map implementation to override
the toString to return that in a URL like style.

As we love contributions, fell free to take a stab at implementing
support for a Map in the HTTP_QUERY header.
Mind that we have several HTTP components that all need to be adjusted :)
- camel-ahc
- camel-http
- camel-http4
- camel-jetty (producer, has shared code with camel-http)

And as always please provide an unit test as well.


> On 7/29/11 5:04 AM, daveharris wrote:
>>
>> Hi Claus,
>> Thanks for the reply.
>>
>> For anyone else searching for the solution, I ended up using a processor
>> to
>> use a javax.ws.rs.core.UriBuilder in jdk6.  My main aim was to get a
>> solution where I didn't have to write the ? and&  which is very brittle.
>>
>> .process(new Processor() {
>>   public void process(Exchange exchange) throws Exception {
>>     Message in = exchange.getIn();
>>
>>     javax.ws.rs.core.UriBuilder uriBuilder =
>> javax.ws.rs.core.UriBuilder.fromUri("http://localhost");
>>     uriBuilder.queryParam("app", in.getHeader(APPLICATION, String.class));
>>     uriBuilder.queryParam("from", in.getHeader(FROM, String.class));
>>     uriBuilder.queryParam("to", in.getHeader(TO, String.class));
>>     uriBuilder.queryParam("msg", in.getBody(String.class));
>>
>>     in.setHeader(Exchange.HTTP_QUERY, uriBuilder.build().getRawQuery());
>>   }
>> })
>>
>> This works but it would be really nice to be able to provide a map ie.
>> in.setHeader(Exchange.HTTP_QUERY, Map&lt;String, String&gt;).
>>
>> I would be happy to have a go at implementing this, unless anyone has any
>> objections.
>>
>> Dave
>>
>> --
>> View this message in context:
>> http://camel.465427.n5.nabble.com/camel-http-Multiple-Request-Paramaters-tp4641050p4644228.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>
>
> --
> Willem
> ----------------------------------
> FuseSource
> Web: http://www.fusesource.com
> Blog:    http://willemjiang.blogspot.com (English)
>         http://jnn.javaeye.com (Chinese)
> Twitter: willemjiang
> Weibo: willemjiang
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: camel-http: Multiple Request Paramaters

Posted by Willem Jiang <wi...@gmail.com>.
If the Map.toString() supports to generate a right query string, I think 
you don't need to change other part camel code.

On 7/29/11 5:04 AM, daveharris wrote:
> Hi Claus,
> Thanks for the reply.
>
> For anyone else searching for the solution, I ended up using a processor to
> use a javax.ws.rs.core.UriBuilder in jdk6.  My main aim was to get a
> solution where I didn't have to write the ? and&  which is very brittle.
>
> .process(new Processor() {
>    public void process(Exchange exchange) throws Exception {
>      Message in = exchange.getIn();
>
>      javax.ws.rs.core.UriBuilder uriBuilder =
> javax.ws.rs.core.UriBuilder.fromUri("http://localhost");
>      uriBuilder.queryParam("app", in.getHeader(APPLICATION, String.class));
>      uriBuilder.queryParam("from", in.getHeader(FROM, String.class));
>      uriBuilder.queryParam("to", in.getHeader(TO, String.class));
>      uriBuilder.queryParam("msg", in.getBody(String.class));
>
>      in.setHeader(Exchange.HTTP_QUERY, uriBuilder.build().getRawQuery());
>    }
> })
>
> This works but it would be really nice to be able to provide a map ie.
> in.setHeader(Exchange.HTTP_QUERY, Map&lt;String, String&gt;).
>
> I would be happy to have a go at implementing this, unless anyone has any
> objections.
>
> Dave
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-http-Multiple-Request-Paramaters-tp4641050p4644228.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>


-- 
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
          http://jnn.javaeye.com (Chinese)
Twitter: willemjiang
Weibo: willemjiang

Re: camel-http: Multiple Request Paramaters

Posted by daveharris <da...@harris.org.nz>.
Hi Claus,
Thanks for the reply.

For anyone else searching for the solution, I ended up using a processor to
use a javax.ws.rs.core.UriBuilder in jdk6.  My main aim was to get a
solution where I didn't have to write the ? and & which is very brittle.

.process(new Processor() {
  public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();

    javax.ws.rs.core.UriBuilder uriBuilder =
javax.ws.rs.core.UriBuilder.fromUri("http://localhost");
    uriBuilder.queryParam("app", in.getHeader(APPLICATION, String.class));
    uriBuilder.queryParam("from", in.getHeader(FROM, String.class));
    uriBuilder.queryParam("to", in.getHeader(TO, String.class));
    uriBuilder.queryParam("msg", in.getBody(String.class));
    
    in.setHeader(Exchange.HTTP_QUERY, uriBuilder.build().getRawQuery());
  }
})

This works but it would be really nice to be able to provide a map ie.
in.setHeader(Exchange.HTTP_QUERY, Map&lt;String, String&gt;).

I would be happy to have a go at implementing this, unless anyone has any
objections.

Dave

--
View this message in context: http://camel.465427.n5.nabble.com/camel-http-Multiple-Request-Paramaters-tp4641050p4644228.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel-http: Multiple Request Paramaters

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Jul 28, 2011 at 4:43 AM, daveharris <da...@harris.org.nz> wrote:
> Hi,
> I am writing a route which sends a HTTP GET request with a bunch of request
> parameters.
>
> Looking at the documentation, the way to do this is via:
> from("direct:start")
>  .setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short"))
>  .to("http://oldhost");
>
> This is fine, but I was hoping for a cleaner way of doing it  -where I don't
> have to handle escaping and &s.
> I've been talking to @Blair and he suggested passing in a map to
> Exchange.HTTP_QUERY but that didn't work.
>
> My route defintion turns into a somewhat ugly-looking:
> from("...")
> .setHeader(Exchange.HTTP_QUERY,
>  constant("app=").append(header(APPLICATION))
>  .append("&from=").append(header(FROM))
>  .append("&to=").append(header(TO))
>  .append("&msg=").append(header(MESSAGE))
> )
> .to("http://localhost/service")
>
> To produce a HTTP get request of
> http://localhost:3000/service?app=test&from=a&to=b&msg=hi
>
> Is this is the only/best way to do it?  Just checking that there isn't some
> shortcut which I can use instead.
>

You can use the recipient list eip
http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html

Then you can use the simple language to refer to the headers in the uri

.recipientList().simple("http://localhost:3000/service?app={{header.APPLICATION}}&from={{header.FROM}}$...")



> The above *works* perfectly well, just being pedantic...
>
> Thanks,
> Dave
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-http-Multiple-Request-Paramaters-tp4641050p4641050.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/