[Reqwest] why use of response.json() will return empty values?

 let req_client = reqwest::Client::new();
    let res = req_client
        .get(format!("https://api.github.com/repos/{}", "rust-lang/rust"))
        .header(USER_AGENT, "GX")
        .send()
        .await?;

 if res.status().is_success() {
      println!("success");
        dbg!(res.json().await?);  // replace with text() will print the body, but json not
    } else {
        println!("Something else happened. Status: {:?}", res.status());
    }

the json method attempts to deserialize a value from the JSON. The return type of the json method is being inferred as (), I think because the return value is being discarded? I'm a little surprised that isn't causing a compile error.

You actually get a deserialization error, not an empty value.

Error: reqwest::Error { kind: Decode, source: Error("invalid type: map, expected unit", line: 1, column: 0) }

If you want to print the JSON data in a nicely formatted way you can use

dbg!(res.json::<serde_json::Value>().await?);

instead.

1 Like