diff --git a/src/async_impl/request.rs b/src/async_impl/request.rs index 504fd56f4..dbeeb581d 100644 --- a/src/async_impl/request.rs +++ b/src/async_impl/request.rs @@ -570,6 +570,31 @@ where } } +impl TryFrom for HttpRequest { + type Error = crate::Error; + + fn try_from(req: Request) -> crate::Result { + let Request { + method, + url, + headers, + body, + version, + .. + } = req; + + let mut req = HttpRequest::builder() + .version(version) + .method(method) + .uri(url.as_str()) + .body(body.unwrap_or_else(Body::empty)) + .map_err(crate::error::builder)?; + + *req.headers_mut() = headers; + Ok(req) + } +} + #[cfg(test)] mod tests { use super::{Client, HttpRequest, Request, Version}; diff --git a/src/wasm/request.rs b/src/wasm/request.rs index 9b36d573a..d45a3a954 100644 --- a/src/wasm/request.rs +++ b/src/wasm/request.rs @@ -1,6 +1,7 @@ use std::convert::TryFrom; use std::fmt; +use bytes::Bytes; use http::{request::Parts, Method, Request as HttpRequest}; use serde::Serialize; #[cfg(feature = "json")] @@ -435,3 +436,26 @@ where }) } } + +impl TryFrom for HttpRequest { + type Error = crate::Error; + + fn try_from(req: Request) -> crate::Result { + let Request { + method, + url, + headers, + body, + .. + } = req; + + let mut req = HttpRequest::builder() + .method(method) + .uri(url.as_str()) + .body(body.unwrap_or_else(|| Body::from(Bytes::default()))) + .map_err(crate::error::builder)?; + + *req.headers_mut() = headers; + Ok(req) + } +}