#reqwest #pretty #json

reqwest-pretty-json

Easy way to emit prettified JSON body via reqwest::RequestBuilder

4 releases

✓ Uses Rust 2018 edition

0.9.0 May 1, 2019
0.1.3 Feb 12, 2019
0.1.2 Nov 12, 2018
0.1.1 Nov 10, 2018

#50 in HTTP client

Download history 45/week @ 2019-01-16 66/week @ 2019-01-23 62/week @ 2019-01-30 74/week @ 2019-02-06 197/week @ 2019-02-13 184/week @ 2019-02-20 123/week @ 2019-02-27 293/week @ 2019-03-06 440/week @ 2019-03-13 252/week @ 2019-03-20 238/week @ 2019-03-27 181/week @ 2019-04-03 224/week @ 2019-04-10 164/week @ 2019-04-17 154/week @ 2019-04-24

861 downloads per month

Apache-2.0 OR MIT

8KB
71 lines

reqwest-pretty-json

reqwest provides an easy way to send a JSON formatted HTTP body of the request. However, it gives you zero control of how the serialization is done. Whatever serde_json::to_vec() emits [single condensed string] is going to be used. In vast majority of the case it is good and does the job very well. However, sometimes you may want to have it "prettified". For example, when talking to KV stores (that just keep the bytes you've sent them and do not interpret it in any way) it may be desirable to have your JSON text more human-readable. There is no way to do it directly with reqwest. Of course you can always manually serialize the data structure into anything you want (pretty JSON) and manually set it as request body as well as adding Content-Type: application/json header. This, however, is less nice than the just letting reqwest::RequestBuilder do the right thing.

Exactly for cases like that this crate augments reqwest::RequestBuilder with pretty_json() method.

use reqwest::Client;
use reqwest_pretty_json::PrettyJson;

let data = vec![1, 2, 3];
let client = Client::new();
client
    .post("http://httpbin.org/post")
    .pretty_json(&data)
    .send()
    .unwrap();

Under the hood it uses serde_json::to_vec_pretty() to serialize the data.

Dependencies

~17MB
~380K SLoC