expression

Decode a typed JSON response

Decode a synthetic, known-small response with the JSON feature. The method consumes the response and may fail if the bytes cannot be decoded into T. It does not replace status, media type, resource or business validation. External crate example; constructs requests or synthetic responses in memory; no request is sent. Expected stdout (not executed): 3 Common mistake: Using full-body JSON decoding for unbounded responses.

Formal Syntax & Structure

rust syntaxrust
response.json::<T>().await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    #[derive(serde::Deserialize)]    struct Book { qty: u32 }    let raw = http::Response::builder().status(200).header("content-type","application/json").body(r#"{"qty":3}"#).unwrap();    let r: reqwest::Response = raw.into();    let book: Book = r.error_for_status().unwrap().json().await.unwrap();    println!("{}", book.qty);}
Rust Reference