Skip to main content

Http

Struct Http 

Source
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:

  1. The handler reads the request body as JSON
  2. The JSON is processed through methods.process_message()
  3. If a response is generated, it’s returned with Content-Type: application/json
  4. 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

Source

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

Source§

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).

Auto Trait Implementations§

§

impl Freeze for Http

§

impl RefUnwindSafe for Http

§

impl Send for Http

§

impl Sync for Http

§

impl Unpin for Http

§

impl UnsafeUnpin for Http

§

impl UnwindSafe for Http

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,