imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.12k stars 334 forks source link

SetCookieJar + SetCookies #329

Open elpaxel opened 4 months ago

elpaxel commented 4 months ago
package main

import (
  "fmt"
  "net/http"
  "net/http/cookiejar"
  "golang.org/x/net/publicsuffix"
  "github.com/imroc/req/v3"
)

func main() {
  jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
  c := req.C().SetCookieJar(jar)
  r := c.R()

  resp, _ := r.Get("https://httpbingo.org/cookies/set?b=2")
  fmt.Println(resp.String())

  r.SetCookies(&http.Cookie{
    Name:   "a",
    Value:  "1",
    Path:   "/",
    Domain: "httpbingo.org",
  }, &http.Cookie{
    Name:   "b",
    Value:  "3",
    Path:   "/",
    Domain: "httpbingo.org",
  })

  resp, _ = r.Get("https://httpbingo.org/cookies")
  fmt.Println(resp.String())
}

Output:

{
  "b": "2"
}

{
  "a": "1",
  "b": "2"
}

I don't think it's worth fixing because I'm happy with the current setup. I just thought that this should be described in the documentation.

I think the reason lies somewhere in the "spread", but I'm not sure which one. First Second