expression

Separate JSON decode errors from HTTP status

A 200 response may contain an unexpected representation. Decode failures are different from transport and status failures. Use synthetic invalid content to isolate this boundary. External crate example; constructs requests or synthetic responses in memory; no request is sent. Expected stdout (not executed): true true Common mistake: Treating a 200 response as a guarantee of valid JSON.

Formal Syntax & Structure

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

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let raw = http::Response::builder().status(200).body("not-json").unwrap();    let r: reqwest::Response = raw.into();    println!("{}", r.status().is_success());    let err = r.json::<serde_json::Value>().await.unwrap_err();    println!("{}", err.is_decode());}
Rust Reference