reqwest_inspect_json

Crates.ioreqwest_inspect_json
lib.rsreqwest_inspect_json
version0.1.0
sourcesrc
created_at2021-04-12 10:12:46.389709
updated_at2022-05-31 09:26:12.872296
descriptionReqwest extention for json response tracing
homepage
repositoryhttps://github.com/DevNulPavel/reqwest_inspect_json
max_upload_size
id382322
size19,364
(DevNulPavel)

documentation

https://docs.rs/crate/reqwest_inspect_json/

README

reqwest-debug-json

Provides inspect_json method for reqwest's response object. The method is replacement for standart json method.

Can be usefull for response json-tracing purposes.

ErrorType must implement both From<reqwest::Error> + From<serde_json::Error>

    .inspect_json::<ResponseStruct, ErrorType>(move |text| {
        debug!("Json data: {}", text);
    })

Extended example:

use reqwest_inspect_json::InspectJson;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
struct TestDataClass {
    key1: String,
    key2: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct Response {
    json: TestDataClass,
}

let test_data = TestDataClass {
    key1: "asdada".to_owned(),
    key2: "asdagfdgdf".to_owned(),
};
let test_data_copy = test_data.clone();

let client = reqwest::Client::new();
let response = client
    .post("http://httpbin.org/post")
    .json(&test_data)
    .send()
    .await
    .expect("Request failed")
    .inspect_json::<Response, DebugError>(move |text| {
        println!("Json content: {}", text);
        let text_data = serde_json::from_str::<Response>(text).expect("Parsing failed");
        assert_eq!(text_data.json, test_data_copy);
    })
    .await
    .expect("Response parse failed");

assert_eq!(response.json, test_data);
Commit count: 14

cargo fmt