Help with reqwest ( <&reqwest::header::HeaderMap )

Apologies if the there is an obvious solution to the below but need some help . I am writting a little application that basically takes a URL , does a GET request and then will write out the code to a file.

I am having difficulty converting the header data into a string. here is the specific bit of code :
fn get(uri: &str , path: &Path) -> Result<(Vec), Box> {
let mut res = reqwest::get(uri)?;
let mut body = String::new();
let status = String::from(res.status().to_string());
let headers = String::from(res.headers());

The two errors i am getting are :
error: src\main.rs:45: the trait bound std::string::String:std::convert::From<&reqwest::header::HeaderMap> is not satisfied
error: src\main.rs:45: the trait std::convert::From<&reqwest::header::HeaderMap> is not implemented for std::string::String

Since i failed on that , i tried to create a Struct that can hold the above data which looked like this:
struct RequestData {
status : String,
header : &reqwest::header::HeaderMap ,
bodydata : String
}

When i attempt to add the header data to the struct i get the following error:
let RequestData1 = RequestData {
status : status ,
header : headers ,
bodydata : body ,
} ;
error: src\main.rs:14: missing lifetime specifier
error: src\main.rs:14: expected lifetime parameter

I have tried various ways of adding lifetimes but failed miserably ( i'm guessing something to do with the &reqwest:: part of the data type which im not sure how to use )

Any help on solving the above would be great !

Do you actually need to store a reference to the header map? The simplest solution is to remove the ampersand, and let the struct own the header map.

You can add the lifetime to the struct like:

struct RequestData<'a> {
    status: String,
    header: &'a reqwest::header::HeaderMap,
    bodydata: String,
}

What are you doing with the headers? How are you outputting them to the file? There might be an easier way to handle them if you don't need to store them.

@cliff : reqwest::Response::headers returns a reference to the header map, so there isn't a way to take ownership of it (short of just keeping ownership of the response.)

Anyway your solution has worked. Thanks a lot

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.