How do I pass cookies from HEAD to POST-Request

285 Views Asked by At

I need to pass csrf cookies _gorila_csrf and csrf_token from my HEAD-Request to my POST-Request. What is the best practice for that? For now I get the Cookie Value with res.Cookies() for the two cookies, built the Cookie-Header Value and pass to the next function. Is there any method to pass the real cookie to the next request?

var c = resp.Cookies()[0].Name+"="+resp.Cookies()[0].Value+";"+resp.Cookies()[1].Name+"="+resp.Cookies()[1].Value
var t = resp.Cookies()[1].Value

Is it possible to address the cookie by name in case more cookies are added in the future and the order changes?

Edit - Example (not working because the api url does not response with the right cookies):

package main

import (
    "fmt"
    "net/http"
    "time"
)

const url = "https://google.com"

func main() {
    client := &http.Client{
        Timeout: time.Second * 5,
    }
    r, _ := http.NewRequest("HEAD", url, nil)
    resp, _ := client.Do(r)

    var c = resp.Cookies()[0].Name + "=" + resp.Cookies()[0].Value + ";" + resp.Cookies()[1].Name + "=" + resp.Cookies()[1].Value
    var t = resp.Cookies()[1].Value

    fmt.Println(c)
    fmt.Println(t)
    }
0

There are 0 best solutions below