|
89 | 89 | //! |
90 | 90 | //! ## Get |
91 | 91 | //! |
92 | | -//! This is a simple example of sending a GET request and printing out |
93 | | -//! the response's body, status code, and reason phrase. The `?` are |
| 92 | +//! This is a simple example of sending a GET request and checking the |
| 93 | +//! response's body, status code, and reason phrase. The `?` are |
94 | 94 | //! needed because the server could return invalid UTF-8 in the body, |
95 | | -//! or something could go wrong during the download. |
| 95 | +//! or something could go wrong while sending the request or receiving |
| 96 | +//! the response. |
96 | 97 | //! |
97 | 98 | //! ``` |
98 | 99 | //! # #[cfg(feature = "std")] |
99 | | -//! # fn main() -> Result<(), Box<dyn std::error::Error>> { |
100 | | -//! let response = bitreq::get("http://example.com").send()?; |
| 100 | +//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { |
| 101 | +//! # use std::thread; |
| 102 | +//! # use tiny_http::{Response, Server}; |
| 103 | +//! # |
| 104 | +//! # let server = Server::http("127.0.0.1:0")?; |
| 105 | +//! # let addr = server.server_addr().to_ip().expect("IP listen addr"); |
| 106 | +//! # let server_thread = thread::spawn(move || { |
| 107 | +//! # let request = server.recv().expect("server recv"); |
| 108 | +//! # let response = Response::from_string("<html></html>"); |
| 109 | +//! # let _ = request.respond(response); |
| 110 | +//! # }); |
| 111 | +//! # |
| 112 | +//! # let url = format!("http://{addr}/"); |
| 113 | +//! let response = bitreq::get(&url).with_timeout(10).send()?; |
101 | 114 | //! assert!(response.as_str()?.contains("</html>")); |
102 | 115 | //! assert_eq!(200, response.status_code); |
103 | 116 | //! assert_eq!("OK", response.reason_phrase); |
| 117 | +//! # server_thread.join().expect("server thread join"); |
104 | 118 | //! # Ok(()) } |
105 | 119 | //! # #[cfg(not(feature = "std"))] |
106 | | -//! # fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) } |
| 120 | +//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { Ok(()) } |
107 | 121 | //! ``` |
108 | 122 | //! |
109 | 123 | //! Note: you could change the `get` function to `head` or `put` or |
|
0 commit comments