1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! # Curio
//!
//! A blazing fast HTTP client.
//!
//! ## Examples:
//!
//! __examples below assume you are importing the `prelude` module as a base dependancy.__
//!
//! # GET content from a url:
//! This one is just a simple GET request, no headers necessary here:
//! ```
//! # use curio::prelude::*;
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let response = Request::get("https://example.com//path/to/resource")
//! .send()?;
//!
//! println!("{:#?}", response);
//! Ok(())
//! }
//! ```
//!
//! # POST tuple content to a url:
//! If the endpoint that you are posting data to supports `application/x-www-form-urlencoded` body structures but you __dont__ want to use a HashMap for whatever reason, this method is the way to go:
//! ```
//! # use curio::prelude::*;
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // lets define a tuple containing key-value pairs.
//! let post_body: Vec<(&str, &str)> = vec!(
//! ("author", "Altrius"),
//! ("timestamp", "Fri, 28 Aug 2020 10:55:44 +0000")
//! );
//!
//! // in this line we convert the tuple into a key-value HashMap.
//! let post_data = PostData::from_tuple(post_body);
//!
//! // below, we set the destination of the post body using the `post` method,
//! // we set the body using the `set_body` method,
//! // and we send the request by using the `send` method
//! let response = Request::post("https://example.com//documents")
//! .set_body(&post_data)
//! .send()?;
//!
//! println!("{:#?}", response);
//! Ok(())
//! }
//! ```
//!
//! # POST HashMap content to a url:
//! If the endpoint that you are posting data to supports `application/x-www-form-urlencoded` body structures, the example below should work pretty well for most users:
//! ```
//! # use std::collections::HashMap;
//! # use curio::prelude::*;
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // lets define a HashMap containing key-value pairs.
//! let mut post_body: HashMap<&str, &str> = HashMap::new();
//! post_body.insert("author", "Altrius");
//! post_body.insert("timestamp", "Fri, 28 Aug 2020 10:55:44 +0000");
//!
//! // in this line we convert the HashMap into a key-value HashMap.
//! let post_data = PostData::from_hash_map(post_body);
//!
//! // below, we set the destination of the post body using the `post` method,
//! // we set the body using the `set_body` method,
//! // and we send the request by using the `send` method
//! let response = Request::post("https://example.com//documents")
//! .set_body(&post_data)
//! .send()?;
//!
//! println!("{:#?}", response);
//! Ok(())
//! }
//! ```
//! # POST plaintext content to a url:
//! Does the endpoint you want to POST data to accept plaintext? the example below might work best for this situation:
//! ```
//! # use curio::prelude::*;
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // lets define a string containing the content body.
//! let mut post_body = "This is some example content to POST";
//!
//! // in this line we convert the string into a format accepted by the `set_body` method.
//! // this method accepts anything which can be converted into a string.
//! let post_data = PostData::from_str(post_body);
//!
//! // below, we set the destination of the post body using the `post` method,
//! // we set the body using the `set_body` method,
//! // and we send the request by using the `send` method
//! let response = Request::post("https://example.com//documents")
//! .set_body(&post_data)
//! .send()?;
//!
//! println!("{:#?}", response);
//! Ok(())
//! }
//! ```