camel-component-http
HTTP client and server component for rust-camel
Overview
The HTTP component provides HTTP client (producer) and HTTP server (consumer) capabilities for rust-camel. Built on reqwest for clients and axum for servers, it enables REST API integration, webhook handling, and HTTP-based messaging.
Features
- HTTP Server (Consumer): Listen for incoming HTTP requests
- HTTP Client (Producer): Make outgoing HTTP requests
- HTTPS Support: Secure connections with
httpsscheme - Configurable Timeouts: Connect and response timeouts
- SSRF Protection: Optional private IP blocking
- Streaming: Direct stream-to-HTTP piping without materialization
- Native Request Streaming: Incoming bodies arrive as
Body::Stream, no RAM materialization - Native Response Streaming:
Body::Streamresponses use chunked transfer encoding automatically - Header Mapping: Automatic header forwarding
- Status Code Handling: Configurable success ranges
Installation
Add to your Cargo.toml:
[]
= "0.2"
URI Format
http://host:port/path[?options]
https://host:port/path[?options]
Consumer Options (Server)
| Option | Default | Description |
|---|---|---|
| Host/path from URI | - | e.g., http://0.0.0.0:8080/api |
maxRequestBody |
2097152 (2 MB) |
If request Content-Length exceeds this value, responds 413 before opening the stream. Chunked uploads without Content-Length are not limited at the consumer level. |
Producer Options (Client)
| Option | Default | Description |
|---|---|---|
httpMethod |
Auto | HTTP method (GET, POST, etc.) |
throwExceptionOnFailure |
true |
Throw on non-2xx responses |
okStatusCodeRange |
200-299 |
Success status code range |
followRedirects |
false |
Follow HTTP redirects |
connectTimeout |
30000 |
Connection timeout (ms) |
responseTimeout |
- | Response timeout (ms) |
allowPrivateIps |
false |
Allow requests to private IPs |
blockedHosts |
- | Comma-separated blocked hosts |
Usage
HTTP Server (Consumer)
use RouteBuilder;
use HttpComponent;
use CamelContext;
let mut ctx = new;
ctx.register_component;
// Simple API endpoint
let route = from
.process
.build?;
HTTP Client (Producer)
// GET request
let route = from
.to
.log
.build?;
// POST request (body becomes request body)
let route = from
.to
.build?;
Dynamic HTTP Method
// Method from header
let route = from
.set_header
.to
.build?;
Dynamic URL
// URL from header
let route = from
.set_header
.to // Base URL, overridden by header
.build?;
HTTPS
let route = from
.to
.build?;
Exchange Headers
Request Headers (Consumer)
| Header | Description |
|---|---|
CamelHttpMethod |
HTTP method (GET, POST, etc.) |
CamelHttpPath |
Request path |
CamelHttpQuery |
Query string |
| All HTTP headers | Forwarded from request |
Response Headers (Producer)
| Header | Description |
|---|---|
CamelHttpResponseCode |
HTTP status code |
CamelHttpResponseText |
Status text |
| All response headers | Forwarded from response |
Request Control Headers (Producer)
| Header | Description |
|---|---|
CamelHttpUri |
Override target URL |
CamelHttpPath |
Append to base URL path |
CamelHttpQuery |
Append query string |
CamelHttpMethod |
Override HTTP method |
Example: REST API Server
use RouteBuilder;
use ;
use CamelContext;
use Body;
async
Example: HTTP Client with Error Handling
let route = from
.to
.build?;
// With custom error handling
let route = from
.error_handler
.to
.process
.build?;
Global Configuration
Configure default HTTP settings in Camel.toml that apply to all HTTP endpoints:
[]
= 5000 # Connection timeout (default: 30000)
= 30000 # Response timeout (default: none)
= 100 # Max concurrent connections (default: 100)
= 10485760 # Max response body size, 10MB (default: 10MB)
= 2097152 # Max request body for server, 2MB (default: 2MB)
= false # Allow requests to private IPs (default: false)
URI parameters always override global defaults:
// Uses global connect_timeout_ms (5000) but overrides allowPrivateIps
.to
// Overrides both global settings
.to
Profile-Specific Configuration
[]
= 30000
[]
= 5000 # Faster fail in production
= false
[]
= 60000 # More lenient in dev
= true # Allow internal services in dev
SSRF Protection
By default, the HTTP client blocks requests to private IP addresses for security. To allow:
.to
To block specific hosts:
.to
Streaming & Memory Management
The HTTP component supports native streaming for both producer and consumer.
Producer (Client) Streaming
The HTTP producer supports streaming request bodies directly without materializing them in memory. Stream bodies are piped to reqwest using wrap_stream().
Memory limits apply when materialization is required (default: 10MB).
Consumer (Server) Streaming
Request Bodies: Incoming HTTP request bodies arrive as Body::Stream in the Exchange, with no RAM materialization by default. The Content-Length header (if present) populates StreamMetadata.size_hint, and Content-Type populates StreamMetadata.content_type.
413 Protection: If the Content-Length header exceeds maxRequestBody, the server responds with HTTP 413 before opening the stream. Chunked uploads without a Content-Length header are not limited at the consumer level.
Response Bodies:
Body::Streamresponses useTransfer-Encoding: chunkedautomatically (no buffering)Body::Bytes/Body::Textresponses use standardContent-Length
Streaming Response Example
// Streaming response example (server-sent data)
from
.process
.build
Request Body Access
// Access request body as stream (default) or materialize it
from
.process
.build
Documentation
License
Apache-2.0
Contributing
Contributions are welcome! Please see the main repository for details.