1

How to post a request to get cookie values and post new request by the previously obtained cookie By using Go language

here first post request is generating a variable cookie in the form [SID=pcmPXXx+fidX1xxxX1cuK; Path=/; HttpOnly; SameSite=Strict]"

but i can't send this cookie to another post request (getting error).

sample go file with comments

package main

import (
  "fmt"
  "log"
  "strings"
  "net/http"
  "io/ioutil"
  "net/http/cookiejar"
)

var client http.Client
func init() {
    jar, err := cookiejar.New(nil)
    if err != nil {
        log.Fatalf("Got error while creating cookie jar %s", err.Error())
    }
    client = http.Client{
        Jar: jar,
    }
}

func main() {

  urlx := "http://localhost:8080/api/v2/auth/login"
  methodx := "POST"

  payloadx := strings.NewReader(`username=admin&password=strongadmin`)

  client := &http.Client {
  }
  reqx, err := http.NewRequest(methodx, urlx, payloadx)

  if err != nil {
    fmt.Println(err)
    return
  }
  reqx.Header.Add("Referer", "http://localhost:8080/")
  reqx.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
  reqx.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")

  resx, err := client.Do(reqx)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer resx.Body.Close()


  cookie := resx.Cookies()
  fmt.Println(cookie)

  bodyx, err := ioutil.ReadAll(resx.Body)
  if err != nil {
    fmt.Println(err)
    return
  }
  fmt.Println(string(bodyx))






  //second request start here 
// this var cookies is a dummy cookie that i manually written,
// i need to replace the below variable 'cookies' to previously obtained variable 'cookie' without error





  cookies := &http.Cookie{
      Name: "SID",
      Value: "PW16gD0Ja0OqixXeqpQle1WC/OK19tj+",
      MaxAge: 300,
  }
  url := "http://localhost:8080/api/changes"
  method := "POST"

  payload := strings.NewReader(`json=%7B%data=valuex%22%3A%22300%22%7D`)

  clientx := &http.Client {
  }
  req, err := http.NewRequest(method, url, payload)

  if err != nil {
    fmt.Println(err)
    return
  }
//  req.Header.Add("Accept", "text/javascript, text/html, application/xml, text/xml, */*")
  req.Header.Add("Referer", "http://localhost:8080/")
  req.Header.Add("X-Requested-With", "XMLHttpRequest")
  req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
  req.Header.Add("Content-type", "application/x-www-form-urlencoded; charset=UTF-8")
  req.AddCookie(cookies)
  res, err := clientx.Do(req)
  if err != nil {
    fmt.Println(err)
    return
  }
  defer res.Body.Close()

  body, err := ioutil.ReadAll(res.Body)
  if err != nil {
    fmt.Println(err)

//second request end here

} 
2
  • You've received raw cookie string, so add it using req.Header.Add("Cookie", cookie) on next request
    – num8er
    Mar 17, 2022 at 16:38
  • replacing req.Header.Add("Cookie", cookie) will give error cannot use cookie (type []*http.Cookie) as type string in argument to req.Header.Add as expected, cause cookie value is in between [ ]
    – Anas S
    Mar 17, 2022 at 16:49

1 Answer 1

1

In first request You're getting cookies as:

cookie := resx.Cookies()
  • which is slice of Cookie struct []*http.Cookie

then if You want to use them in next request do loop over them and add:

for _, c := range cookie {
  req.AddCookie(c)
}

check this example

2
  • 1
    ok thanks buddy you just saved my another 12 hours, one last doubt what is the difference between for _, c :=... and for c :=...
    – Anas S
    Mar 17, 2022 at 17:13
  • 1
    @AnasS read here for better understanding go.dev/play/p/fINUtdV9AZa
    – num8er
    Mar 17, 2022 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.