ureq icon indicating copy to clipboard operation
ureq copied to clipboard

Getting the list of all the headers and their content

Open nponsard opened this issue 2 years ago • 1 comments

Hello,

I'm working on an API code generator. I want all the generated functions to return an access to all the headers and the parsed body.

To return all the headers I currently loop over response.headers_names() and then read the response.header(&name) :

pub(crate) fn get_header_map(response: &ureq::Response) -> HashMap<String, String> {
    let mut headers = HashMap::new();

    let names = response.headers_names();
    for name in names {
        if let Some(value) = response.header(&name) {
            headers.insert(name, value.to_string());
        }
    }
    headers
}

I find it very inefficient, the most efficient way from my point of view would be to add an access to the headers in the Response struct.

Do you think this would cause problems ? I can try to open a PR adding this function.

Edit: I found the "http-interop" feature, this could give me the access I want but I don't like the fact that unwrap() is called on into_string().

nponsard avatar Sep 06 '23 11:09 nponsard

I got same problem.

But I prefer get_headers() -> &[Header], this is how it was stored in response after all. Then we can do other things on &[Header]

flaribbit avatar Oct 31 '23 01:10 flaribbit

Closing since we're moving to ureq 3.x. This is solved in ureq 3.x

algesten avatar Aug 13 '24 18:08 algesten