You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Steppacher Ralf <ra...@derivativepartners.com> on 2013/04/10 16:03:33 UTC

Dynamically built URLs and URL encoding

Hi,

this is about Camel 2.10.3...

I need to dynamically build the URL/query string to be passed to the http component. As far as I can see there are two ways of achieving this:

- Set header CamelHttpQuery
<camel:setHeader headerName="CamelHttpQuery">
  <camel:simple>fields=${headers[field.list]}&amp;instrument=${headers[instrument.list]}</camel:simple>
</camel:setHeader>

- Use the recipient list
<camel:recipientList delimiter="@">
  <camel:simple>http:host/path?fields=${headers[field.list]}&amp;instrument=${headers[instrument.list]}</camel:simple>
</camel:recipientList>

For me only the first option works because "field.list" and "instrument.list" are comma separated lists that need to be send to the server as they are. When I use the recipient list there is no way to prevent the whole query string from being URL encoded (which encodes the commata in the lists). On the other hand, when setting the query string via the exchange header there is no way (I am aware of) to tell Camel to URL encode the query string if need be.

My question is whether I am relying on a bug or a feature?

In HttpProducer::createMethod(Exchange) Line 358 there is a comment that says "need to make sure the queryString is URI safe", but then the query string is passed straight into org.apache.commons.httpclient.HttpMethod::setQueryString(String), which does not do any URL encoding itself but relies on the caller to do what's needed.


Thanks!
Ralf

RE: Dynamically built URLs and URL encoding

Posted by Steppacher Ralf <ra...@derivativepartners.com>.
Hi Christian,

What I want to achieve is to send a request to an http endpoint. The URI has this form:

http:host.com/somepath?listparam1=<comma-separated-list_1>&listparam2=<comma-separated-list_1>

With every request the two list parameters change their value, thus I needed a way to generate the URI dynamically. I found the two ways below of doing so, but as you say the list is possibly not complete. The recipient list I use purely for its dynamic URI building capabilities; the delimiter I provide is guaranteed to not show up in my URI.

The problem I was facing was that when built with the recipient list, the comma separated lists get URL encoded, turning the commas into %2C, which the server does not like. I debugged through it and saw no way of preventing the URL encoding by way of configuration.
Setting the Exchange header CamelHttpQuery with a simple expression allows me to achieve the dynamic query string building as well. This time I found no way of enabling URL encoding. Not that I need it, I need it to work as it works now, without URL encoding. But again I debugged through the code and found this suspicious comment in HttpProducer::createMethod(Exchange) (Line 358), that to me suggests that the behavior of the HttpProducer regarding URL encoding should be different. Or will be different in the future. 

In other words, I am not sure whether not to URL encode the query string in the HttpProducer is a bug or a feature. (I want it to be a feature ;-)). The documentation on the web for the recipient list and the HTTP component do not mention URL encoding at all...


Thanks!
Ralf

________________________________________
From: Christian Müller [christian.mueller@gmail.com]
Sent: Thursday, April 11, 2013 00:04
To: users@camel.apache.org
Subject: Re: Dynamically built URLs and URL encoding

To be correct, you have a few more options. Also have a look at [1], [2]
and [3].

I'm not sure whether I understood what you want to do. With the recipient
list, you can provide a comma (or whatever you want) separated list of
endpoint which should receive the message. Like:
http://www.google.de/search?q=camel,http://search.yahoo.com/search?p=camel

In this case, both endpoints are called with the same payload.

[1] http://camel.apache.org/routing-slip.html
[2] http://camel.apache.org/dynamic-router.html
[3] http://camel.apache.org/producertemplate.html


On Wed, Apr 10, 2013 at 4:03 PM, Steppacher Ralf <
ralf.steppacher@derivativepartners.com> wrote:

> Hi,
>
> this is about Camel 2.10.3...
>
> I need to dynamically build the URL/query string to be passed to the http
> component. As far as I can see there are two ways of achieving this:
>
> - Set header CamelHttpQuery
> <camel:setHeader headerName="CamelHttpQuery">
>
> <camel:simple>fields=${headers[field.list]}&amp;instrument=${headers[instrument.list]}</camel:simple>
> </camel:setHeader>
>
> - Use the recipient list
> <camel:recipientList delimiter="@">
>
> <camel:simple>http:host/path?fields=${headers[field.list]}&amp;instrument=${headers[instrument.list]}</camel:simple>
> </camel:recipientList>
>
> For me only the first option works because "field.list" and
> "instrument.list" are comma separated lists that need to be send to the
> server as they are. When I use the recipient list there is no way to
> prevent the whole query string from being URL encoded (which encodes the
> commata in the lists). On the other hand, when setting the query string via
> the exchange header there is no way (I am aware of) to tell Camel to URL
> encode the query string if need be.
>
> My question is whether I am relying on a bug or a feature?
>
> In HttpProducer::createMethod(Exchange) Line 358 there is a comment that
> says "need to make sure the queryString is URI safe", but then the query
> string is passed straight into
> org.apache.commons.httpclient.HttpMethod::setQueryString(String), which
> does not do any URL encoding itself but relies on the caller to do what's
> needed.
>
>
> Thanks!
> Ralf
>

Re: Dynamically built URLs and URL encoding

Posted by Christian Müller <ch...@gmail.com>.
To be correct, you have a few more options. Also have a look at [1], [2]
and [3].

I'm not sure whether I understood what you want to do. With the recipient
list, you can provide a comma (or whatever you want) separated list of
endpoint which should receive the message. Like:
http://www.google.de/search?q=camel,http://search.yahoo.com/search?p=camel

In this case, both endpoints are called with the same payload.

[1] http://camel.apache.org/routing-slip.html
[2] http://camel.apache.org/dynamic-router.html
[3] http://camel.apache.org/producertemplate.html


On Wed, Apr 10, 2013 at 4:03 PM, Steppacher Ralf <
ralf.steppacher@derivativepartners.com> wrote:

> Hi,
>
> this is about Camel 2.10.3...
>
> I need to dynamically build the URL/query string to be passed to the http
> component. As far as I can see there are two ways of achieving this:
>
> - Set header CamelHttpQuery
> <camel:setHeader headerName="CamelHttpQuery">
>
> <camel:simple>fields=${headers[field.list]}&amp;instrument=${headers[instrument.list]}</camel:simple>
> </camel:setHeader>
>
> - Use the recipient list
> <camel:recipientList delimiter="@">
>
> <camel:simple>http:host/path?fields=${headers[field.list]}&amp;instrument=${headers[instrument.list]}</camel:simple>
> </camel:recipientList>
>
> For me only the first option works because "field.list" and
> "instrument.list" are comma separated lists that need to be send to the
> server as they are. When I use the recipient list there is no way to
> prevent the whole query string from being URL encoded (which encodes the
> commata in the lists). On the other hand, when setting the query string via
> the exchange header there is no way (I am aware of) to tell Camel to URL
> encode the query string if need be.
>
> My question is whether I am relying on a bug or a feature?
>
> In HttpProducer::createMethod(Exchange) Line 358 there is a comment that
> says "need to make sure the queryString is URI safe", but then the query
> string is passed straight into
> org.apache.commons.httpclient.HttpMethod::setQueryString(String), which
> does not do any URL encoding itself but relies on the caller to do what's
> needed.
>
>
> Thanks!
> Ralf
>