lorraine batol
lorraine batol

Reputation: 6281

apache camel - call an external web service

I would like to send an http post to an external web service that I need to call.

<from uri="cxfrs://http://localhost:9876?resourceClasses=MyResource"/>
            <log message="Received. " loggingLevel="INFO" logName="MyLogger"/>
            <setHeader headerName="CamelHttpMethod">
                 <constant>POST</constant>
            </setHeader>
            <setHeader headerName="Content-Type">
                <constant>application/json</constant>
            </setHeader>
            <setBody>
                <simple>param1=param1value&amp;param2=param2value</simple>
            </setBody>
<to uri="http://samplesample.com?bridgeEndpoint=true" />
<log message="body is ${body}" loggingLevel="INFO" logName="MyLogger"/>

I'm getting an exception:

Caused by: org.apache.camel.component.http.HttpOperationFailedException: HTTP operation failed invoking http://samplesample.com with statusCode: 400

It works fine when I launch a request using rest client. Any help will be appreciated.

Upvotes: 0

Views: 1682

Answers (2)

Peter Keller
Peter Keller

Reputation: 7636

Instead of putting your request parameters to the message body you should set the appropriate headers, e.g. for adding HTTP querying parameters use

<setHeader headerName="CamelHttpQuery">
 <constant>param1=param1value&amp;param2=param2value</constant>
</setHeader>

or for adding HTTP path parameters use

<setHeader headerName="CamelHttpPath">
 <constant>/param1/20</constant>
</setHeader>

Upvotes: 0

Willem Jiang
Willem Jiang

Reputation: 3291

camel-cxfrs consumer will turn the REST request into a method call, so the camel-http producer may not interpreter the message rightly.

If you want to proxy REST request by using camel, you can just use camel-jetty component to do that.

from("jetty://http://localhost:9876?matchOnUriPrefix=true")
                    .to("http://samplesample.com?throwExceptionOnFailure=false&bridgeEndpoint=true");

Upvotes: 2

Related Questions