HTTP Cookie Parser
Parse Set-Cookie headers and cookie strings into structured key-value pairs with attribute details.
Paste a Set-Cookie header or cookie string to parse.
Frequently Asked Questions
Code Implementation
from http.cookies import SimpleCookie
from datetime import datetime
def parse_set_cookie(header: str) -> dict:
"""Parse a Set-Cookie header string."""
cookie = SimpleCookie()
cookie.load(header)
result = {}
for name, morsel in cookie.items():
result[name] = {
"value": morsel.value,
"path": morsel["path"] or "/",
"domain": morsel["domain"],
"expires": morsel["expires"],
"max_age": morsel["max-age"],
"secure": morsel["secure"],
"httponly": morsel["httponly"],
"samesite": morsel["samesite"],
}
return result
# Example
header = "session=abc123; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=3600"
parsed = parse_set_cookie(header)
print(parsed)
# Build a cookie header
def build_set_cookie(name: str, value: str, **attrs) -> str:
parts = [f"{name}={value}"]
for k, v in attrs.items():
if v is True:
parts.append(k.replace('_', '-'))
elif v:
parts.append(f"{k.replace('_', '-')}={v}")
return "; ".join(parts)
cookie = build_set_cookie("token", "xyz789",
Path="/", Max_Age=3600, HttpOnly=True, Secure=True, SameSite="Lax")
print(cookie)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.