expression
Tonic: message and request metadata
A Request carries a typed message plus call metadata. Use a harmless synthetic request identifier to correlate work. Metadata is supplied by the caller and is not trusted proof of identity. Run in the accompanying generated-bindings Cargo project. Expected stdout (not executed): demo-42: 7 Common mistake: Treating a caller-controlled request ID as authentication.
Formal Syntax & Structure
rust syntaxrust
Request::new(message); request.metadata_mut().insert(key, value)Working Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { use bm_tonic_inventory::pb::GetItemRequest; use tonic::Request; let mut req = Request::new(GetItemRequest { id: 7 }); req.metadata_mut().insert("x-request-id", "demo-42".parse().unwrap()); println!("{}: {}", req.metadata().get("x-request-id").unwrap().to_str().unwrap(), req.get_ref().id);}