Reqwest - Transfering SetCookie from a Response to a Request


#1

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 ?


Implementing From<SetCookie> for Cookie for hyper
#2
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.


#3

@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…


#4

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.


#5

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?


#6

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:.


#7

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


#8

I think that I have discovered something unexpected… Nice!