tonic icon indicating copy to clipboard operation
tonic copied to clipboard

feat(transport): Add disable user-agent

Open i18nsite opened this issue 5 months ago • 0 comments

fix https://github.com/hyperium/tonic/issues/970

disable default feature can without any user-agent

in your Cargo.toml

tonic = { version = "0.13.1", default-features = false, features = [
  "channel",
  "codegen",
  "prost",
] }

if want set, use with_interceptor like


use bytes::Bytes;
use tonic::{
  service::{Interceptor, interceptor::InterceptedService},
  transport::{Channel, Endpoint, Error},
};

use crate::api_client::ApiClient;

pub async fn conn<F>(
  addr: impl Into<Bytes>,
  interceptor: F,
) -> Result<ApiClient<InterceptedService<Channel, F>>, Error>
where
  F: Interceptor,
{
  let endpoint = Endpoint::from_shared(addr.into())?;
  let channel = endpoint.connect().await?;

  let client = ApiClient::with_interceptor(channel, interceptor);

  Ok(client)
}
    let mut client = conn("12.3.2.3:1111", |mut req: tonic::Request<()>| {
        req.metadata_mut().insert("user-agent", "xxxx".try_into().unwrap());
        Ok(req)
    });


i18nsite avatar Jul 08 '25 15:07 i18nsite