http icon indicating copy to clipboard operation
http copied to clipboard

What is the recommended way of copying a `Request`?

Open costa opened this issue 4 years ago • 5 comments

Headers and body in particular.

Thanks.

costa avatar May 01 '20 15:05 costa

My current "solution" for Uri rewriting:

fn req_sub_uri(req: Request<Body>, uri: Uri) -> Request<Body> {
    let mut req = Request::builder()
        .method(req.method())
        .uri(uri)
        .body(*req.body())
        .unwrap();
    let headers = req.headers_mut();
    for (name, val) in req.headers() {
        headers.insert(name, *val);
    }
    req
}

However, this doesn't look "Rusty".

costa avatar May 01 '20 16:05 costa

fn req_sub_uri(mut req: Request<Body>, uri: Uri) -> Request<Body> {
    *req.uri_mut() = uri;
    req
}

sfackler avatar May 01 '20 16:05 sfackler

@sfackler This is not good enough, unfortunately, for me, because Client#request needs to move the request and I cannot reuse it.

costa avatar May 01 '20 16:05 costa

@sfackler Thank you anyway, I ended up optimising the flow and using #uri_mut.

Though my immediate problem is gone, this is not exactly a workaround, and I would really like this issue resolved.

costa avatar May 02 '20 14:05 costa

Is there still no way to do this??

im trying to write some middleware to retry a failed request. This requires that you copy the request before calling it, but that doesnt seem to be possible with the current implementation. Am I correct? this is a very common usecase

stan-irl avatar May 10 '23 09:05 stan-irl