2

I'm trying to dynamically create a RESTful URL based on the incoming XML:

Input:

<message>
   <request name="John"/>
</message>

I want to route it like this:

http://myhost:8080/myservice?name=John

I know how to set URL parameters using setHeader but I don't know how to extract the attribute values from the input message. The SOOPER_XPATH expression I'm looking for might look like this:

from("direct:start")
  .setHeader(Exchange.HTTP_QUERY,
     simple("name=$SOOPER_XPATH(${in.body}, '//request/@name')")
  .to("http://myhost:8080/myservice").log("Received response: ${in.body});
2
  • 3
    have you tried this XPath : '//request/@name'? Commented May 14, 2014 at 2:54
  • Indeed tThe xpath in the snippet had a typo, //request@name instead of //request/@name, sorry about that. But my problem is not with the xpath expression. Commented May 16, 2014 at 23:36

2 Answers 2

1

There are probably several ways to do it, this is what we do in one of my projects, works fine:

exchange.getIn().setHeader(XPathBuilder.xpath("/message/messageID/text()").evaluate(exchange,String.class));

We do this inside a processor, but you could probably use the XPathBuilder directly in a route as well.

It's really just a matter of getting the xpath right. I usually use some tool like http://www.xpathtester.com/xpath to test my xpaths.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I haven't been able to figure out how to use it in a route. Using processor instead.
0

In order to set the header in the route outside of a Processor, use:

from("direct:start")
    .setHeader(Exchange.HTTP_QUERY, xpath("//request@name").stringResult())
    .to("http://myhost:8080/myservice").log("Received response: ${in.body}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.