opstalj
opstalj

Reputation: 900

Apache Camel: how extract parameter from incoming HTTP message (with XML body)

I am trying to use CAMEL as HTTP proxy and I would like to extract a parameter from an incoming HTTP message with a XML body. This parameter I would then like to add in the header of a HTTP POST message towards another endpoint (another server).

Example: the XML body contains a parameter called "subscriptionId". The value of this field "subscriptionId" is then to be used in the uri of the outgoing HTTP POST message.

So, if subscriptionId=1234567, I want the uri in the HTTP POST message to be like:

POST /webapp/createnewsubscription?subscriptionId=1234567

I am using Spring DSL to create my Camel routes.

Anyone an idea how to do this ?

Thanks,

Jan

Upvotes: 2

Views: 10203

Answers (1)

maximdim
maximdim

Reputation: 8169

I presume you want to POST to first URL with XML as payload.

First you would need to use XPath component to get value for your XML tag and then setBody to pass parameter to proxied request (optionally you could switch from POST to GET).

Something like this should work:

<route>
  <from uri="jetty:http://127.0.0.1:8080/myapp"/>
  <setHeader headerName="subscriptionId">
    <xpath resultType="java.lang.String">//subscriptionId/text()</xpath>
  </setHeader>
  <!-- if you need to convert from POST to GET
  <setHeader headerName="CamelHttpMethod">
    <constant>GET</constant>
  </setHeader>
   -->
  <setBody> 
    <simple>subscriptionId=${in.headers.subscriptionId}</simple> 
 </setBody> 
  <to uri="jetty:http://127.0.0.1:8090/myapp?bridgeEndpoint=true&amp;throwExceptionOnFailure=false"/>
</route>

You should be able to test it from command line say with wget:

$ cat 1.txt
<a>
<subscriptionId>123</subscriptionId>
</a>

$ wget --post-file=1.txt --header="Content-Type:text/xml" http://127.0.0.1:8080/myapp

You could use second route to test responses like this:

<route>
  <from uri="jetty:http://127.0.0.1:8090/myapp"/>
  <to uri="log:mylog?level=INFO"/>
  <setBody>
    <simple>OK: ${in.headers.CamelHttpMethod}: ${in.headers.subscriptionId}</simple>
  </setBody>      
</route>

And if you set camelContext to 'trace' you should see lots of info in your log of what's going on on every step of the processing:

<camel:camelContext id="camel" trace="true" xmlns="http://camel.apache.org/schema/spring">

Upvotes: 2

Related Questions