0

I 'm trying to use camel for redirecting an http request based on the contents of the request body.

I 've created an endpoint for receiving http requests and I 'm able to successfully redirect my request to a different URL, using the following route;

<route>
    <from uri="servlet:myapptest2?matchOnUriPrefix=true"/>  
    <to uri="http://sampleurl/test?bridgeEndpoint=true&amp;throwExceptionOnFailure=false" />
</route>

But I 'm not able to figure out a way to check the http request body. My HTTP request will be having a JSON body similar to this;

{
    "testLoginRequest": {
            "sampleData1": [{
                    "key1": "val1",
                    "key2": "val2"          
            }],
            "sampleData2": [{
                    "key1": "val1",
                    "key2": "val2"
            }]
    }
}

I want to redirect the request to a specific URL if the JSON contains the value "testLoginRequest". I read that this should be possible like ;

from("direct:test"). 
    choice().
    when(body().contains("javainuse1"))
    .to("jms:queue:javainuse1").       
    otherwise().
    to("jms:queue:otherwise");

Since I have to use the XML DSL, I tried implementing the following route, but it doesn't seem to work;

<route>
    <from uri="servlet:myapptest3?matchOnUriPrefix=true" /> 
    <choice>
        <when>
            <simple>${body} contains 'testLoginRequest' </simple>
            <to uri="http://sampleurlforlogin/test?bridgeEndpoint=true&amp;throwExceptionOnFailure=false" />
        </when>                         
    </choice>
</route>

Am I following the right approach. What am I doing wrong here?

2
  • 1
    Your body may be an inputstream. So try to use "${bodyAs(String)}" instead of "${body}" in your simple expression Commented Jul 11, 2019 at 15:46
  • @TacheDeChoco This worked. Thank you. Commented Jul 16, 2019 at 6:06

0

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.