pub struct Http { /* private fields */ }Expand description
HTTP-based transport for JSON-RPC messages.
This transport uses axum to handle HTTP POST requests with JSON-RPC messages. Each HTTP POST request is treated as a JSON-RPC request, and the response is returned in the HTTP response body.
§Architecture
When a request arrives via HTTP POST:
- The handler reads the request body as JSON
- The JSON is processed through
methods.process_message() - If a response is generated, it’s returned with Content-Type: application/json
- If no response is needed (notification), an empty 200 OK is returned
This design is much simpler than channel-based approaches because HTTP is inherently request/response - we don’t need to manage pending responses or correlate requests with responses.
§Example
use json_rpc::{Http, Methods};
use serde_json::Value;
use std::net::SocketAddr;
async fn echo(params: Value) -> Result<Value, json_rpc::Error> {
Ok(params)
}
let methods = Methods::new().add("echo", echo);
let addr: SocketAddr = "127.0.0.1:3000".parse().unwrap();
let transport = Http::new(addr);
json_rpc::serve(transport, methods).await.unwrap();Implementations§
Source§impl Http
impl Http
Sourcepub fn new(address: SocketAddr) -> Self
pub fn new(address: SocketAddr) -> Self
Create a new HTTP transport with the specified bind address.
The server will accept POST requests at /jsonrpc on the specified address.
§Example
use json_rpc::Http;
use std::net::SocketAddr;
// Default address
let addr: SocketAddr = "127.0.0.1:3000".parse().unwrap();
let transport = Http::new(addr);
// Custom address
let addr: SocketAddr = "127.0.0.1:8080".parse().unwrap();
let transport = Http::new(addr);Trait Implementations§
Source§impl Transport for Http
impl Transport for Http
Source§async fn serve(self, methods: Methods) -> Result<(), Error>
async fn serve(self, methods: Methods) -> Result<(), Error>
Serve the JSON-RPC server using HTTP transport.
This method starts an axum HTTP server that accepts POST requests
at /jsonrpc. Each request is processed as a JSON-RPC message and
the response is returned in the HTTP response.
The server runs until it is shut down (e.g., by Ctrl+C).