Skip to content

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

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

0x676e67
Copy link
Contributor

@0x676e67 0x676e67 commented Aug 14, 2025

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
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
/// Extension trait for http::Response objects
///
/// Provides methods to extract URL information from HTTP responses
pub trait ResponseExt {
Copy link
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
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
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
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)
    }
}

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.

2 participants