pub struct Request<T> { /* private fields */ }
Expand description

A gRPC request and metadata from an RPC call.

Implementations

Create a new gRPC request.

Request::new(HelloRequest {
   name: "Bob".into(),
});

Get a reference to the message

Get a mutable reference to the message

Get a reference to the custom request metadata.

Get a mutable reference to the request metadata.

Consumes self, returning the message

Convert an HTTP request to a gRPC request

Get the remote address of this connection.

This will return None if the IO type used does not implement Connected. This currently, only works on the server side.

Get the peer certificates of the connected client.

This is used to fetch the certificates from the TLS session and is mostly used for mTLS. This currently only returns Some on the server side of the transport server with TLS enabled connections.

Set the max duration the request is allowed to take.

Requires the server to support the grpc-timeout metadata, which Tonic does.

The duration will be formatted according to the spec and use the most precise unit possible.

Example:

use std::time::Duration;
use tonic::Request;

let mut request = Request::new(());

request.set_timeout(Duration::from_secs(30));

let value = request.metadata().get("grpc-timeout").unwrap();

assert_eq!(
    value,
    // equivalent to 30 seconds
    "30000000u"
);

Returns a reference to the associated extensions.

Returns a mutable reference to the associated extensions.

Example

Extensions can be set in interceptors:

use tonic::{Request, service::interceptor};

struct MyExtension {
    some_piece_of_data: String,
}

interceptor(|mut request: Request<()>| {
    request.extensions_mut().insert(MyExtension {
        some_piece_of_data: "foo".to_string(),
    });

    Ok(request)
});

And picked up by RPCs:

use tonic::{async_trait, Status, Request, Response};

#[async_trait]
impl TestService for MyService {
    async fn handler(&self, req: Request<Input>) -> Result<Response<Output>, Status> {
        let value: &MyExtension = req.extensions().get::<MyExtension>().unwrap();

        Ok(Response::new(Output {}))
    }
}

Trait Implementations

Formats the value using the given formatter. Read more
Wrap the input message T in a tonic::Request
The RPC request stream type
The RPC request type
Wrap the stream of messages in a tonic::Request

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more