...
- response code is between 100..299 then Camel regard it as a success response
- response code is between 300..399 then Camel regard it as a redirection was returned and will throw a HttpOperationFailedException with the information
- response code is 400+ then Camel regard it as a external server failure and will throw a HttpOperationFailedException with the information
Tip title throwExceptionOnFailure The option
throwExceptionOnFailure
can be set tofalse
to prevent the HttpOperationFailedException to be thrown for failed response codes. This allows you to get any response from the remote server.
There is a sample below demonstrating this.
HttpOperationFailedException
...
You can set the http producer's URI directly form the endpoint URI. In the route below Camel will call our to the external server oldhost using HTTP.
Code Block |
---|
from("direct:start")
.to("http://oldhost");
|
...
In Camel 1.5.1 you can override the http endpoint URI by adding a header with the key HttpProducer.HTTP_URI
on the message.
Code Block |
---|
from("direct:start")
.setHeader(org.apache.camel.component.http.HttpProducer.HTTP_URI, constant("http://newhost"))
.to("http://oldhost");
|
In the sample above Camel will call the http://newhost despite the endpoint is configured with http://oldhost
.
And the same code in Camel 2.0:
Code Block |
---|
from("direct:start")
.setHeader(HttpConstants.HTTP_URI, constant("http://newhost"))
.to("http://oldhost");
|
...
The http producer supports URI parameters to be sent to the HTTP server. The URI parameters can either be set directly on the endpoint URI or as a header with the key HttpProducer.QUERY
on the message.
Code Block |
---|
from("direct:start")
.to("http://oldhost?order=123&detail=short");
|
Or options provided in a header:
Code Block |
---|
from("direct:start")
.setHeader(HttpConstants.HTTP_QUERY, constant("order=123&detail=short"))
.to("http://oldhost");
|
...
The HTTP component provides a way to set the HTTP request method by setting the message header. Here is an example;
Code Block |
---|
from("direct:start") .setHeader(HttpConstants.HTTP_METHOD, constant(org.apache.camel.component.http.HttpMethods.POST)) .to("http://www.google.com") .to("mock:results"); |
The method can be written a bit shorter using the string constants:
Code Block |
---|
.setHeader("CamelHttpMethod", constant("POST"))
|
...
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext xmlns="http://activemq.apache.org/camel/schema/spring">
<route>
<from uri="direct:start"/>
<setHeader headerName="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<to uri="http://www.google.com"/>
<to uri="mock:results"/>
</route>
</camelContext>
|
Configuring a Proxy
Only for >= Camel 1.6.2
The HTTP component provides a way to configure a proxy.
Code Block |
---|
from("direct:start")
.to("http://oldhost?proxyHost=www.myproxy.com&proxyPort=80");
|
There is also support for proxy authentication via the proxyUsername and proxyPassword options.
Configuring charset
If you are using POST to send data you can configure the charset using the Exchange property:
Code Block |
---|
exchange.setProperty(Exchange.CHARSET_NAME, "iso-8859-1");
|
...
You can get the http response code from the http component by getting the value from out message header with HttpProducer.HTTP_RESPONSE_CODE.
Code Block | ||||
---|---|---|---|---|
| ||||
Exchange exchange = template.send("http://www.google.com/search", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setHeader(HttpProducer.QUERY, constant("hl=en&q=activemq"));
}
});
Message out = exchange.getOut();
int responseCode = out.getHeader(HttpProducer.HTTP_RESPONSE_CODE, Integer.class);
|
...