Reqwest - Transfering SetCookie from a Response to a Request

Hello,

I'm currently struggling with passing the content of a SetCookie to a Cookie (you can find the precise implementation of the issue here) :
I successfully retrieved the content of the SetCookie (l. 65) of a POST request for using it in the header field of a latter request (l.71), the problem is that when I try to set the header field Cookie with the previous retrieved content the compiler sends back "[rustc] expected function, found struct Cookie ".

Could somebody solve/explain this to me ?

let mut setcookie: Vec<String> = Vec::new();
headers.set(Cookie(setcookie));
error[E0423]: expected function, found struct `Cookie`

There is no function called Cookie that takes an argument of type Vec<String>. Cookie is a tuple struct but its fields are private and not of type Vec<String>.


I would write this as:

let mut cookie = Cookie::new();
if let Some(&SetCookie(ref content)) = post_request.headers().get() {
    for def in content {
        /* parse Set-Cookie string */
        /* call cookie.append(k, v) */
    }
}
let mut headers = Headers::new();
headers.set(cookie);

See SetCookie for some examples of what the strings look like.

1 Like

@dtolnay Thank you for taking your time to answer my question!
I didn't fully understood how should I access the key and the value in your for loop though...

It sounds like you have to parse the key/value pairs out of the string based on the schema @dtolnay linked to. There must be a good reason for it, but it's unclear why that API doesn't encapsulate that for you.

Do you mean that reqwest is missing an impl From<SetCookie> for Cookie?

If I understand my orphan rules correctly, only reqwest itself could provide that impl, correct?

I meant why there's no API to iterate over the key/value pairs - i.e. why does the caller have to do manual string parsing :slight_smile:.

1 Like

Ah, like that!
Sounds like a nice feature request then!

I think that I have discovered something unexpected... Nice!