diff --git a/src/async_impl/request.rs b/src/async_impl/request.rs index 665710430..387a09ca4 100644 --- a/src/async_impl/request.rs +++ b/src/async_impl/request.rs @@ -622,6 +622,30 @@ where } } +impl TryFrom> for Request { + type Error = crate::Error; + + fn try_from(req: HttpRequest<()>) -> crate::Result { + let (parts, _) = req.into_parts(); + let Parts { + method, + uri, + headers, + version, + .. + } = parts; + let url = Url::parse(&uri.to_string()).map_err(crate::error::builder)?; + Ok(Request { + method, + url, + headers, + body: None, + timeout: None, + version, + }) + } +} + impl TryFrom for HttpRequest { type Error = crate::Error; @@ -890,6 +914,17 @@ mod tests { assert_eq!(req.url().as_str(), "http://localhost/"); } + #[test] + fn convert_from_http_request_without_body() { + let http_request = HttpRequest::builder() + .method("GET") + .uri("http://localhost/") + .body(()) + .unwrap(); + let req: Request = Request::try_from(http_request).unwrap(); + assert!(req.body().is_none()); + } + #[test] fn set_http_request_version() { let http_request = HttpRequest::builder() diff --git a/src/blocking/request.rs b/src/blocking/request.rs index 94f89b46a..72f232881 100644 --- a/src/blocking/request.rs +++ b/src/blocking/request.rs @@ -640,6 +640,24 @@ where } } +impl TryFrom> for Request { + type Error = crate::Error; + + fn try_from(req: HttpRequest<()>) -> crate::Result { + let (parts, body) = req.into_parts(); + let Parts { + method, + uri, + headers, + .. + } = parts; + let url = Url::parse(&uri.to_string()).map_err(crate::error::builder)?; + let mut inner = async_impl::Request::new(method, url); + crate::util::replace_headers(inner.headers_mut(), headers); + Ok(Request { body: None, inner }) + } +} + impl fmt::Debug for Request { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt_request_fields(&mut f.debug_struct("Request"), self).finish() @@ -1006,6 +1024,17 @@ mod tests { assert_eq!(req.url().as_str(), "http://localhost/"); } + #[test] + fn convert_from_http_request_without_body() { + let http_request = HttpRequest::builder() + .method("GET") + .uri("http://localhost/") + .body(()) + .unwrap(); + let req: Request = Request::try_from(http_request).unwrap(); + assert!(req.body().is_none()); + } + #[test] fn set_http_request_version() { let http_request = HttpRequest::builder() diff --git a/src/wasm/request.rs b/src/wasm/request.rs index c373e5c8f..b7b849cf3 100644 --- a/src/wasm/request.rs +++ b/src/wasm/request.rs @@ -455,6 +455,29 @@ where } } +impl TryFrom> for Request { + type Error = crate::Error; + + fn try_from(req: HttpRequest<()>) -> crate::Result { + let (parts, body) = req.into_parts(); + let Parts { + method, + uri, + headers, + .. + } = parts; + let url = Url::parse(&uri.to_string()).map_err(crate::error::builder)?; + Ok(Request { + method, + url, + headers, + body: None, + cors: true, + credentials: None, + }) + } +} + impl TryFrom for HttpRequest { type Error = crate::Error;