Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convertion from Response to http::Response<Vec<u8>> #638

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/http_interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ impl From<Response> for http::Response<String> {
}
}

/// Converts a [Response](crate::Response) into an [http::Response], where the
/// body is a Vec<u8>.
///
/// Due to slight differences in how headers are handled, this means if a header
/// from a [Response](crate::Response) is not valid UTF-8, it will not be
/// included in the resulting [http::Response].
///
/// Requires feature `ureq = { version = "*", features = ["http"] }`
/// ```
/// # fn main() -> Result<(), ureq::Error> {
/// # ureq::is_test(true);
/// let response = ureq::get("http://example.com").call()?;
/// let http_response: http::Response<Vec<u8>> = response.into();
/// # Ok(())
/// # }
/// ```
impl From<Response> for http::Response<Vec<u8>> {
fn from(value: Response) -> Self {
create_builder(&value)
.body(value.into_string().unwrap().into_bytes())
.unwrap()
}
}

/// Converts an [http] [Builder](http::request::Builder) into a [Request](crate::Request)
///
/// This will safely handle cases where a builder is not fully "complete" to
Expand Down