Contents

Add camel-http-starter for Spring Boot projects. It pulls in Apache HttpClient 5 transitively.

<!-- Spring Boot --> <dependency> <groupId>org.apache.camel.springboot</groupId> <artifactId>camel-http-starter</artifactId> </dependency> <!-- Standalone Camel --> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-http</artifactId> </dependency>

Use to("https://...") or to("http://...") to send a GET request. The response body becomes the Camel exchange body. The HTTP status code is stored in the Exchange.HTTP_RESPONSE_CODE header.

import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component; @Component public class HttpGetRoute extends RouteBuilder { @Override public void configure() { from("timer:poll?period=10000") .setHeader(Exchange.HTTP_METHOD, constant("GET")) .to("https://api.example.com/users?bridgeEndpoint=true") .log("Response [${header.CamelHttpResponseCode}]: ${body}"); } } bridgeEndpoint=true tells Camel not to interpret the URI as a Camel endpoint address — it passes the full URL directly to HttpClient. Always set this when calling external URLs.

Set the Exchange.HTTP_METHOD header to POST and put a JSON string (or serialized object) in the exchange body. Set Content-Type to application/json so the server accepts the payload.

import org.apache.camel.Exchange; import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component; @Component public class HttpPostRoute extends RouteBuilder { @Override public void configure() { from("direct:createUser") // body is the JSON payload set by the caller .setHeader(Exchange.HTTP_METHOD, constant("POST")) .setHeader(Exchange.CONTENT_TYPE, constant("application/json")) .to("https://api.example.com/users?bridgeEndpoint=true") .choice() .when(header(Exchange.HTTP_RESPONSE_CODE).isEqualTo(201)) .log("User created: ${body}") .otherwise() .log("Unexpected status: ${header.CamelHttpResponseCode}") .end(); } } // Caller: inject JSON body before sending to direct:createUser producerTemplate.sendBody("direct:createUser", "{\"name\":\"Alice\",\"email\":\"alice@example.com\"}");

Append query parameters directly to the URL, or set them as exchange headers using Exchange.HTTP_QUERY. Use .setHeader() to add custom HTTP request headers such as Authorization.

from("direct:searchProducts") // Custom API key header .setHeader("X-Api-Key", constant("my-secret-key")) // Authorization header .setHeader("Authorization", simple("Bearer ${exchangeProperty.token}")) // Dynamic query string — overrides any query in the URL .setHeader(Exchange.HTTP_QUERY, simple("page=${header.page}&size=${header.size}&category=${header.category}")) .setHeader(Exchange.HTTP_METHOD, constant("GET")) .to("https://api.example.com/products?bridgeEndpoint=true") .log("Products: ${body}"); Headers whose names start with Camel are Camel internal headers and are not forwarded to the HTTP server. All other headers set on the exchange are forwarded as HTTP request headers. Use Exchange.HTTP_QUERY to build a dynamic query string without embedding it in the URI.

For public CA-signed certificates no extra configuration is needed — the JVM's default trust store covers them. For self-signed or internal certificates, configure an SSLContextParameters bean and reference it on the endpoint.

import org.apache.camel.support.jsse.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SslConfig { @Bean public SSLContextParameters sslContextParameters() { KeyStoreParameters ksp = new KeyStoreParameters(); ksp.setResource("classpath:keystore.jks"); ksp.setPassword("keystore-pass"); KeyManagersParameters kmp = new KeyManagersParameters(); kmp.setKeyStore(ksp); kmp.setKeyPassword("key-pass"); TrustManagersParameters tmp = new TrustManagersParameters(); KeyStoreParameters tsp = new KeyStoreParameters(); tsp.setResource("classpath:truststore.jks"); tsp.setPassword("trust-pass"); tmp.setKeyStore(tsp); SSLContextParameters scp = new SSLContextParameters(); scp.setKeyManagers(kmp); scp.setTrustManagers(tmp); return scp; } } // Reference the bean in the route via #beanRef syntax from("direct:secureCall") .to("https://internal.corp.com/api/data" + "?bridgeEndpoint=true" + "&sslContextParameters=#sslContextParameters");

To trust all certificates during development (never in production):

SSLContextParameters scp = new SSLContextParameters(); scp.setSecureSocketProtocol("TLSv1.3"); TrustManagersParameters tmp = new TrustManagersParameters(); tmp.setTrustManager(new X509TrustManager() { public void checkClientTrusted(X509Certificate[] c, String a) {} public void checkServerTrusted(X509Certificate[] c, String a) {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } }); scp.setTrustManagers(tmp);

Combine Camel's onException DSL with maximumRedeliveries to retry on transient HTTP errors (5xx, connection timeouts). Use HttpOperationFailedException to catch non-2xx responses.

import org.apache.camel.component.http.HttpOperationFailedException; import org.apache.camel.builder.RouteBuilder; import org.springframework.stereotype.Component; @Component public class HttpRetryRoute extends RouteBuilder { @Override public void configure() { onException(HttpOperationFailedException.class) .onWhen(exceptionMessage().contains("500") .or(exceptionMessage().contains("503"))) .maximumRedeliveries(3) .redeliveryDelay(2000) // 2 s initial delay .backOffMultiplier(2) // exponential: 2s, 4s, 8s .useExponentialBackOff() .retryAttemptedLogLevel(org.apache.camel.LoggingLevel.WARN) .handled(true) .log("All retries exhausted: ${exception.message}"); from("direct:callApi") .setHeader(Exchange.HTTP_METHOD, constant("GET")) .to("https://api.example.com/data?bridgeEndpoint=true") .log("Success: ${body}"); } } Set throwExceptionOnFailure=true (the default) on the HTTP endpoint so Camel raises HttpOperationFailedException for non-2xx responses — otherwise non-2xx responses are silently delivered as the exchange body.
OptionDefaultDescription
bridgeEndpointfalsePass the full URL to HttpClient without Camel interpreting it. Always set to true for external URLs.
throwExceptionOnFailuretrueThrow HttpOperationFailedException for non-2xx HTTP responses.
connectTimeoutConnection timeout in milliseconds.
socketTimeoutSocket read timeout in milliseconds.
followRedirectsfalseAutomatically follow 3xx redirects.
authUsername / authPasswordBasic auth credentials.
sslContextParametersReference to an SSLContextParameters bean (use #beanName).
copyHeaderstrueCopy response HTTP headers back into the exchange.
disableStreamCachefalseDisable response body caching — useful for large streaming responses.