Skip to content

Commit 7e2ce64

Browse files
pintarichingpintariching
andauthored
Add PUT and DELETE methods to Request struct (#73)
Adds `put` and `delete` methods to `Request`. Also adds `put_json` method under the `json` feature. Fixes issue #72 . # --------- Co-authored-by: pintariching <tilen@pintaric.si>
1 parent a2ca266 commit 7e2ce64

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

ehttp/src/types.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,34 @@ impl Request {
197197
}
198198
}
199199

200+
/// Create a 'PUT' request with the given url and body.
201+
#[allow(clippy::needless_pass_by_value)]
202+
pub fn put(url: impl ToString, body: Vec<u8>) -> Self {
203+
Self {
204+
method: "PUT".to_owned(),
205+
url: url.to_string(),
206+
body,
207+
headers: Headers::new(&[
208+
("Accept", "*/*"),
209+
("Content-Type", "text/plain; charset=utf-8"),
210+
]),
211+
mode: Mode::default(),
212+
timeout: Some(Self::DEFAULT_TIMEOUT),
213+
}
214+
}
215+
216+
/// Create a 'DELETE' request with the given url.
217+
pub fn delete(url: &str) -> Self {
218+
Self {
219+
method: "DELETE".to_owned(),
220+
url: url.to_string(),
221+
body: vec![],
222+
headers: Headers::new(&[("Accept", "*/*")]),
223+
mode: Mode::default(),
224+
timeout: Some(Self::DEFAULT_TIMEOUT),
225+
}
226+
}
227+
200228
/// Multipart HTTP for both native and WASM.
201229
///
202230
/// Requires the `multipart` feature to be enabled.
@@ -250,6 +278,23 @@ impl Request {
250278
})
251279
}
252280

281+
#[cfg(feature = "json")]
282+
/// Create a 'PUT' request with the given url and json body.
283+
#[allow(clippy::needless_pass_by_value)]
284+
pub fn put_json<T>(url: impl ToString, body: &T) -> serde_json::error::Result<Self>
285+
where
286+
T: ?Sized + Serialize,
287+
{
288+
Ok(Self {
289+
method: "PUT".to_owned(),
290+
url: url.to_string(),
291+
body: serde_json::to_string(body)?.into_bytes(),
292+
headers: Headers::new(&[("Accept", "*/*"), ("Content-Type", "application/json")]),
293+
mode: Mode::default(),
294+
timeout: Some(Self::DEFAULT_TIMEOUT),
295+
})
296+
}
297+
253298
pub fn with_timeout(mut self, timeout: Option<Duration>) -> Self {
254299
self.timeout = timeout;
255300
self

0 commit comments

Comments
 (0)