What is the Set-Cookie header?
The Set-Cookie response header, defined in RFC 6265, tells the client to store a cookie and send it back via the Cookie header on future requests. Each cookie carries a name/value pair plus optional attributes controlling its scope and lifetime.
API Usage & Best Practices
- Lock down access:
HttpOnlyhides the cookie from JavaScript, andSecurerestricts it to HTTPS connections, reducing theft and interception risks. - Defend against CSRF:
SameSite(Strict,Lax, orNone) controls whether the cookie is sent on cross-site requests;NonerequiresSecure. - Scope and expiry:
DomainandPathlimit where the cookie is sent, whileMax-Age(orExpires) sets how long it persists.
Examples
HTTP/1.1 200 OKSet-Cookie: session_id=abc123; Path=/; Domain=example.com; Max-Age=3600; HttpOnly; Secure; SameSite=Lax