cookie

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package cookie parses and serializes HTTP cookie headers.

It is a port of the npm package "cookie". Parse reads a "Cookie" request header into a map, URL-decoding values. Serialize produces a "Set-Cookie" header value with the given attributes, URL-encoding the value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(header string) map[string]string

Parse parses a Cookie header (for example "a=1; b=2") into a map of names to values. Values are URL-decoded. When a name appears more than once, the first occurrence wins.

func Serialize

func Serialize(name, value string, opts *Options) (string, error)

Serialize builds a Set-Cookie header value for the given name and value, applying the attributes in opts (which may be nil). The value is URL-encoded. It returns an error if the name or value contains invalid characters, or if an attribute value is invalid.

Types

type Options

type Options struct {
	// Path sets the Path attribute.
	Path string
	// Domain sets the Domain attribute.
	Domain string
	// Expires sets the Expires attribute. A zero time omits the attribute.
	Expires time.Time
	// MaxAge sets the Max-Age attribute. Following net/http conventions:
	// a positive value sets Max-Age to that number of seconds, a negative
	// value sets Max-Age=0 (delete now), and zero omits the attribute.
	MaxAge int
	// Secure sets the Secure attribute.
	Secure bool
	// HttpOnly sets the HttpOnly attribute.
	HttpOnly bool
	// SameSite sets the SameSite attribute; recognized values are "Lax",
	// "Strict", and "None" (case-insensitive). An empty value omits it.
	SameSite string
}

Options holds the attributes used when serializing a cookie.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL