Search code examples
flurl

How to get a response cookie with flurl


How to get a response cookie with flurl? I've searched for some references and studied on flurl.dev but still confused how to apply them. sorry I am not a programmer, I still have a lot to learn. Simple code that i use :

var strUrl = await url
.WithHeaders(new
{
    user_agent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
    content_type = "application/json",
    cookie = "cookie"
})
.PostJsonAsync(new
{
    user = "user",
    password = "password"
})
.ReceiveString();

Result : 200 OK


Solution

  • The problem here is that when you call ReceiveString(), you're getting the response body and effectively discarding the other aspects of the response message returned by PostJsonAsync. You can get the response, cookies, and body in separate steps like this:

    var resp = await url
        .WithHeaders(...)
        .PostJsonAsync(...);
        
    var cookies = resp.Cookies; // list of FlurlCookie objects
    var body = await resp.GetStringAsync();