开发者

Apache Camel: URI Escaping in HTTP (and other Producers)

开发者 https://www.devze.com 2023-03-16 17:00 出处:网络
In an Apache Camel route, I want to take several headers and compose them into an HTTP query string in a safe way. The only examples I\'ve found either use constant(), which isn\'t useful for building

In an Apache Camel route, I want to take several headers and compose them into an HTTP query string in a safe way. The only examples I've found either use constant(), which isn't useful for building dynamic query strings, or they use simple() which doesn't offer URL escaping.

For example, take the following snippet right from HTTP component's documentation:

   from("direct:start")
      .setHeader(Exchange.HTTP_QUERY, constant("order=123&detail=short"))
      .to("http://oldhost");

This is 90% of the way there, but what if you don't always want order id 123? We'd like to be able to substitute a header value here. So, the next logical version of this is to switch to simple:

   from("direct:start")
    .setHeader(Exchange.HTTP_QUERY, simple("order=${header.orderId}&detail=short"))
    .to("http://oldhost");

But this has the major issue of not being URL encoded. This means that a space (or any reserved character) in header.orderId results in an exception thrown by the HTTP component for an invalid query string.

So the only way that's left is to use JavaScript, which is very verbose for something like this, or to write a custom processor. It seems like this should be something that's built-in, s开发者_开发技巧o I'm asking here to see if I'm missing an obvious/normal way to do what I'm looking for here?


see http://camel.465427.n5.nabble.com/URI-Escaping-in-HTTP-and-other-Producers-td4427457.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号