Skip to content

feat(response): preserve URL when converting Response to http::Response#2798

Open
0x676e67 wants to merge 5 commits into
seanmonstar:masterfrom
0x676e67:resp
Open

feat(response): preserve URL when converting Response to http::Response#2798
0x676e67 wants to merge 5 commits into
seanmonstar:masterfrom
0x676e67:resp

Conversation

@0x676e67

@0x676e67 0x676e67 commented Aug 14, 2025

Copy link
Copy Markdown
Contributor

Add URL preservation in the From for http::Response conversion by
inserting the response URL as a ResponseUrl extension. This ensures that the original
URL information is not lost during the conversion and can be accessed later.

This change maintains consistency with the inverse conversion (Fromhttp::Response
for Response) which expects the ResponseUrl extension to be present.

@0x676e67 0x676e67 marked this pull request as ready for review August 14, 2025 08:36
@0x676e67 0x676e67 marked this pull request as draft August 14, 2025 08:47
@0x676e67

Copy link
Copy Markdown
Contributor Author

If this suggestion is adopted, to be honest, I’m not sure how the ResponseExt API should be designed to align with most people’s usage habits — things like:

/// Extension trait for http::Response objects
///
/// Provides methods to extract URL information from HTTP responses
pub trait ResponseExt {
    /// Extracts the URL associated with this response
    fn url(&self) -> Option<&Url>;

    /// Extracts a mutable reference to the URL associated with this response
    fn url_mut(&mut self) -> Option<&mut Url>;

    /// Takes the URL from the response, consuming it and returning the URL
    fn take_url(&mut self) -> Option<Url>;
}

@0x676e67 0x676e67 marked this pull request as ready for review August 14, 2025 12:01
Comment thread src/response.rs
/// Extension trait for http::Response objects
///
/// Provides methods to extract URL information from HTTP responses
pub trait ResponseExt {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking it might be better to leave this off. Is there a need for it that I'm overlooking?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did run into some scenarios where I needed to convert a reqwest::Response into an http::Response while preserving the original URL. That said, as you pointed out, it's always possible to manually clone the URL using the url() method on reqwest::Response. While that approach is a bit more verbose, the only benefit of this change would be to avoid one additional clone, which probably isn't significant in practice.

So I agree with your suggestion—this functionality isn't strictly necessary at the moment. I'm fine with closing this PR, and if a stronger need arises in the future, we can revisit it then.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't mean we need to close the whole thing... I actually have for a while thought we should just store the url in the extensions instead of directly on the struct...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, it's still possible to get the URL with an implementation like this:

impl From<Response> for (Url, http::Response<Body>) {
    fn from(r: Response) -> (Url, http::Response<Body>) {
        let (parts, body) = r.res.into_parts();
        let body = Body::wrap(body);
        let response = http::Response::from_parts(parts, body);
        (*r.url, response)
    }
}

@jakemarsden

jakemarsden commented Jun 10, 2026

Copy link
Copy Markdown

I would find this useful. My use-case is adding some instrumentation to the response body stream in a reqwest_middleware::Middleware layer, although presumably modifying an existing response in any way (while preserving the URL) would require something similar?

I'd like to do this, but currently the URL is lost:

let (parts, body) = http::Response::from(res).into_parts();

let wrapped_body = MyWrapperStream::wrap(http_body_util::BodyDataStream::new(body));

Response::from(http::Response::from_parts(
    parts,
    Body::wrap_stream(wrapped_body),
))

Currently I'm doing this:

let url = res.url().clone();
let (parts, body) = http::Response::from(res).into_parts();

let mut new_res = http::Response::builder()
    .status(parts.status)
    .version(parts.version)
    .url(url);
if let Some(headers) = new_res.headers_mut() {
    *headers = parts.headers;
}
if let Some(extensions) = new_res.extensions_mut() {
    extensions.extend(parts.extensions);
}

let wrapped_body = MyWrapperStream::wrap(http_body_util::BodyDataStream::new(body));

new_res
    .body(Body::wrap_stream(wrapped_body))
    .map_err(reqwest_middleware::Error::middleware)
    .map(Response::from)

Which is fine, it's just one extra clone and a bit more verbose.

Or maybe there's already a better way to do it that I'm missing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants