1
+ use serde:: Serialize ;
2
+
1
3
use crate :: http:: headers:: { HeaderName , ToHeaderValues } ;
2
4
use crate :: http:: { Body , Mime , StatusCode } ;
3
5
use crate :: Response ;
@@ -53,6 +55,9 @@ impl ResponseBuilder {
53
55
}
54
56
55
57
/// Sets the Content-Type header on the response.
58
+ ///
59
+ /// # Examples
60
+ ///
56
61
/// ```
57
62
/// # use tide::{http::mime, Response};
58
63
/// let response = Response::builder(200).content_type(mime::HTML).build();
@@ -64,6 +69,9 @@ impl ResponseBuilder {
64
69
}
65
70
66
71
/// Sets the body of the response.
72
+ ///
73
+ /// # Examples
74
+ ///
67
75
/// ```
68
76
/// # async_std::task::block_on(async move {
69
77
/// # use tide::{Response, convert::json};
@@ -75,6 +83,107 @@ impl ResponseBuilder {
75
83
self . 0 . set_body ( body) ;
76
84
self
77
85
}
86
+
87
+ /// Pass JSON as the response body.
88
+ ///
89
+ /// # Mime
90
+ ///
91
+ /// The `Content-Type` is set to `application/json`.
92
+ ///
93
+ /// # Errors
94
+ ///
95
+ /// This method will return an error if the provided data could not be serialized to JSON.
96
+ ///
97
+ /// # Examples
98
+ ///
99
+ /// ```no_run
100
+ /// # use serde::{Deserialize, Serialize};
101
+ /// # use tide::Response;
102
+ /// # #[async_std::main]
103
+ /// # async fn main() -> tide::Result<()> {
104
+ /// #[derive(Deserialize, Serialize)]
105
+ /// struct Ip {
106
+ /// ip: String
107
+ /// }
108
+ ///
109
+ /// let data = &Ip { ip: "129.0.0.1".into() };
110
+ /// let res = Response::builder(200).body_json(data)?.build();
111
+ /// assert_eq!(res.status(), 200);
112
+ /// # Ok(()) }
113
+ /// ```
114
+ pub fn body_json ( self , json : & impl Serialize ) -> crate :: Result < Self > {
115
+ Ok ( self . body ( Body :: from_json ( json) ?) )
116
+ }
117
+
118
+ /// Pass a string as the response body.
119
+ ///
120
+ /// # Mime
121
+ ///
122
+ /// The `Content-Type` is set to `text/plain; charset=utf-8`.
123
+ ///
124
+ /// # Examples
125
+ ///
126
+ /// ```no_run
127
+ /// # use tide::Response;
128
+ /// # #[async_std::main]
129
+ /// # async fn main() -> tide::Result<()> {
130
+ /// let data = "hello world".to_string();
131
+ /// let res = Response::builder(200).body_string(data).build();
132
+ /// assert_eq!(res.status(), 200);
133
+ /// # Ok(()) }
134
+ /// ```
135
+ pub fn body_string ( self , string : String ) -> Self {
136
+ self . body ( Body :: from_string ( string) )
137
+ }
138
+
139
+ /// Pass bytes as the response body.
140
+ ///
141
+ /// # Mime
142
+ ///
143
+ /// The `Content-Type` is set to `application/octet-stream`.
144
+ ///
145
+ /// # Examples
146
+ ///
147
+ /// ```no_run
148
+ /// # use tide::Response;
149
+ /// # #[async_std::main]
150
+ /// # async fn main() -> tide::Result<()> {
151
+ /// let data = b"hello world".to_owned();
152
+ /// let res = Response::builder(200).body_bytes(data).build();
153
+ /// assert_eq!(res.status(), 200);
154
+ /// # Ok(()) }
155
+ /// ```
156
+ pub fn body_bytes ( self , bytes : impl AsRef < [ u8 ] > ) -> Self {
157
+ self . body ( Body :: from ( bytes. as_ref ( ) ) )
158
+ }
159
+
160
+ /// Pass a file as the response body.
161
+ ///
162
+ /// # Mime
163
+ ///
164
+ /// The `Content-Type` is set based on the file extension using [`mime_guess`] if the operation was
165
+ /// successful. If `path` has no extension, or its extension has no known MIME type mapping,
166
+ /// then `None` is returned.
167
+ ///
168
+ /// [`mime_guess`]: https://docs.rs/mime_guess
169
+ ///
170
+ /// # Errors
171
+ ///
172
+ /// This method will return an error if the file couldn't be read.
173
+ ///
174
+ /// # Examples
175
+ ///
176
+ /// ```no_run
177
+ /// # use tide::Response;
178
+ /// # #[async_std::main]
179
+ /// # async fn main() -> tide::Result<()> {
180
+ /// let res = Response::builder(200).body_file("./archive.tgz").await?.build();
181
+ /// assert_eq!(res.status(), 200);
182
+ /// # Ok(()) }
183
+ /// ```
184
+ pub async fn body_file ( self , path : impl AsRef < std:: path:: Path > ) -> std:: io:: Result < Self > {
185
+ Ok ( self . body ( Body :: from_file ( path) . await ?) )
186
+ }
78
187
}
79
188
80
189
impl From < ResponseBuilder > for Response {
0 commit comments