Standard library / net/http/cookiejar

cookiejar.Jar.Cookies()

Select cookies for a URL based on stored domain, path, secure and expiration rules. Unsupported schemes return an empty slice. Expected stdout: '1 0\n'.

Formal Syntax & Structure

go syntaxgo
func (j *Jar) Cookies(u *url.URL) (cookies []*http.Cookie)

Parameters & Attributes

NameTypeDescription
u*url.URLURL whose cookie scope is used.

Return Value

(cookies []*http.Cookie)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "net/http/cookiejar"  "net/url")
func main() {  j := must(cookiejar.New(nil))  u := must(url.Parse("https://example.invalid/a"))  j.SetCookies(u, []*http.Cookie{{Name: "demo", Value: "yes", Secure: true}})  fmt.Println(len(j.Cookies(u)), len(j.Cookies(must(url.Parse("http://example.invalid/a")))))}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference