sagebind / isahc

The practical HTTP client that is fun to use.
https://docs.rs/isahc
MIT License
705 stars 62 forks source link

Cookie Jar is not sent despite it's existance #429

Closed DjakaTechnology closed 1 year ago

DjakaTechnology commented 1 year ago

Hello, i'm trying to get cookie working but somehow the cookie never sent in the header, but somehow it never sent in the request.

let req = isahc::Request::get(url)
        .header(USER_AGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
        .auto_referer()
        .cookie_jar(ISHAC_COOKIE_JAR.clone())
        .body(()).expect("Hardcoded value");
    println!("{:#?}", req);
    println!("{:#?}", ISHAC_COOKIE_JAR.clone());
    println!("{:#?}", Vec::from_iter(ISHAC_COOKIE_JAR.get_for_uri(&Uri::from_str(url).unwrap())));

Weirdly, when we get via ISHAC_COOKIE_JAR.get_for_uri(&Uri::from_str(url) it managed to return the cookie. Is there missing config in my code?

Thanks

Log:

Request {
    method: GET,
    uri: https://foo.com/balblalba,
    version: HTTP/1.1,
    headers: {
        "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
    },
    body: (),
}
CookieJar {
    cookies: RwLock {
        data: {
            CookieWithContext {
                domain_value: "foo.com",
                path_value: "/",
                cookie: Cookie {
                    name: "newsletterPopupCount",
                    value: "1",
                    domain: None,
                    path: Some(
                        "/",
                    ),
                    secure: false,
                    expiration: Some(
                        SystemTime {
                            tv_sec: 1690958168,
                            tv_nsec: 141483000,
                        },
                    ),
                },
            },
            CookieWithContext {
                domain_value: "foo.com",
                path_value: "/",
                cookie: Cookie {
                    name: "viewType",
                    value: "direct",
                    domain: None,
                    path: Some(
                        "/",
                    ),
                    secure: false,
                    expiration: None,
                },
            },
        },
        poisoned: false,
        ..
    },
}
[
    Cookie {
        name: "newsletterPopupCount",
        value: "1",
        domain: None,
        path: Some(
            "/",
        ),
        secure: false,
        expiration: Some(
            SystemTime {
                tv_sec: 1690958168,
                tv_nsec: 141483000,
            },
        ),
    },
    Cookie {
        name: "viewType",
        value: "direct",
        domain: None,
        path: Some(
            "/",
        ),
        secure: false,
        expiration: None,
    },
]
sagebind commented 1 year ago

Hi, thanks for reaching out!

At first glance it looks like the problem is that you never send the request you built. Once you build the request using body(()), you need to send the request using send. Attaching a cookie jar using cookie_jar will not immediately apply a Cookie header to the request object, it just associates the cookie jar with the request. You have to send the request for the Isahc cookie engine to take any action.

If you want to see the actual headers that get sent in a request I recommend you hook up a log-compatible logger (such as env_logger) and enable trace logs for the isahc::wire log target. Isahc will log the actual headers that get written to the network there. Debug-printing the Request object will only show headers that you've explicitly supplied to the request object. The request object is just a plain old data structure, it doesn't contain any actual logic (until you send it).

Hope that helps!

DjakaTechnology commented 1 year ago

Ahh i see, my bad then. Thank you!