BrwaKurdish
BrwaKurdish

Reputation: 33

How can I extract a certain header from http response [Set-Cookie]

I've searched around google for this, but I haven't been able to fix this problem yet, so I'm sending a post http request to a certain website, and in return I get this headers

map[Cache-Control:[no-cache] Content-Type:[application/json; charset=utf-8] Date:[Mon, 19 Oct 2020 15:38:41 GMT] Expires:[-1] P3p:[CP="CAO DSP COR CURa ADMa DEVa OUR IND PHY ONL UNI COM NAV INT DEM PRE"] Pragma:[no-cache] Site-Machine-Id:[CHI1-WEB5027] Set-Cookie:[.ROBL=A6251611CFF4513169C4CED10EFDC2DE9444E207CA376B27B906306AC18BFB0ACA6F17381240973900B19186F3E46C6BC9C52B99BC040579110E87145209A47040B241FE2C702C18AEF12A1AC746812B22596BFDB33C24DF1D5CEC72705DAD266343EC259528D8B7617BBE17408A957DF7A1C2CC7AC9DD9CC05FF8F4831BCC1669FB5221A74E6DB5C8EE0ED7F8F4AFA3767CCC39D919A62C6800EFFFF812DED5325F68D36B410D86A0CAB1FB0B8A90ADD529BE75A2DAFD3EB59D86BBC831C3144E577357B8EB0C514D0433F0B8E69DA151E6BA2C63968B46184167CAE05FE6B4749DC0449C71BB80A1306C6699E9EBD79E4C6A348CC33418D3E0DC3E6F5; expires=Wed, 12-Oct-2050 15:38:42 GMT; path=/; HttpOnly .RBID=eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIyY2Q5N2IyOS01MjgxLTRjMWQtYjgxMS03OTQzNWZkNzU0ZjkiLCJzdWIiOjcxNjQ3MzgxOH0.yg9EiXLF4VY2O7Eu5mTdbax60tMrodiPbADWwRwZMeo; expires=Thu, 17-Oct-2030 15:38:42 GMT; path=/; secure; HttpOnly Data=UserID=-733325636; expires=Fri, 06-Mar-2048 16:38:42 GMT; path=/ REventTrackerV2=CreateDate=10/19/2020 10:38:42 AM&rbxid=&browserid=65376450118; expires=Fri, 06-Mar-2048 16:38:42 GMT; path=/] Vary:[Accept-Encoding] X-Frame-Options:[SAMEORIGIN]]

I want to extract .ROBL from, [Set-Cookie] just doing res.Header.Get(".ROBL") doesn't seem to be doing the job.

I tried to do split := strings.Split(string(header), ";") but that panics on fail so it's not relaible

Is there any relaible ways to extract .ROBL from [Set-Cookie] in the header?

Upvotes: 0

Views: 1086

Answers (1)

icza
icza

Reputation: 417622

Cookies are sent with the Set-Cookie HTTP header, so you can't simply get them as Header.Get("cookie-name"). You would have to parse the Set-Cookie header values. But the standard lib does this for you:

Cookies sent by a server may be parsed using Response.Cookies(). It returns you a slice of cookies (http.Cookie), just iterate over them until you find the one you're looking for.

cookies := resp.Cookies()
for _, c := range cookies {
    if c.Name == ".ROBL" {
        fmt.Println(c)
        fmt.Println(c.Value)
    }
}

Also note that if you want cookie management, you should consider using a CookieJar. For details, see What is the difference between cookie and cookiejar?

Upvotes: 3

Related Questions