mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-27 14:57:46 +00:00
Update examples again.
This commit is contained in:
parent
cd9571615a
commit
94d28bdfbb
1 changed files with 26 additions and 17 deletions
43
README.md
43
README.md
|
|
@ -34,15 +34,20 @@ Or install it yourself as:
|
||||||
# Load from a file
|
# Load from a file
|
||||||
jar.load(filename) if File.exist?(filename)
|
jar.load(filename) if File.exist?(filename)
|
||||||
|
|
||||||
# Store received cookies
|
# Store received cookies, where uri is the origin of this header
|
||||||
jar.parse(set_cookie_header_value, uri)
|
header["Set-Cookie"].each { |value|
|
||||||
|
jar.parse(value, uri)
|
||||||
|
}
|
||||||
|
|
||||||
# Get the value for the Cookie field of a request header
|
# ...
|
||||||
cookie_header_value = HTTP::Cookie.cookie_value(jar.cookies(uri))
|
|
||||||
|
# Set the Cookie header value, where uri is the destination URI
|
||||||
|
header["Cookie"] = HTTP::Cookie.cookie_value(jar.cookies(uri))
|
||||||
|
|
||||||
# Save to a file
|
# Save to a file
|
||||||
jar.save(filename)
|
jar.save(filename)
|
||||||
|
|
||||||
|
|
||||||
########################
|
########################
|
||||||
# Client side example 2
|
# Client side example 2
|
||||||
########################
|
########################
|
||||||
|
|
@ -50,35 +55,39 @@ Or install it yourself as:
|
||||||
# Initialize a cookie jar using a Mozilla compatible SQLite3 backend
|
# Initialize a cookie jar using a Mozilla compatible SQLite3 backend
|
||||||
jar = HTTP::CookieJar.new(store: :mozilla, filename: 'cookies.sqlite')
|
jar = HTTP::CookieJar.new(store: :mozilla, filename: 'cookies.sqlite')
|
||||||
|
|
||||||
# Store received cookies
|
|
||||||
jar.parse(set_cookie_header_value, uri)
|
|
||||||
|
|
||||||
# Get the value for the Cookie field of a request header
|
|
||||||
cookie_header_value = HTTP::Cookie.cookie_value(jar.cookies(uri))
|
|
||||||
|
|
||||||
# There is no need for load & save in this backend.
|
# There is no need for load & save in this backend.
|
||||||
|
|
||||||
|
# Store received cookies, where uri is the origin of this header
|
||||||
|
header["Set-Cookie"].each { |value|
|
||||||
|
jar.parse(value, uri)
|
||||||
|
}
|
||||||
|
|
||||||
|
# ...
|
||||||
|
|
||||||
|
# Set the Cookie header value, where uri is the destination URI
|
||||||
|
header["Cookie"] = HTTP::Cookie.cookie_value(jar.cookies(uri))
|
||||||
|
|
||||||
|
|
||||||
########################
|
########################
|
||||||
# Server side example
|
# Server side example
|
||||||
########################
|
########################
|
||||||
|
|
||||||
# Generate a domain cookie
|
# Generate a domain cookie
|
||||||
cookie2 = HTTP::Cookie.new("uid", "u12345", domain: 'example.org',
|
cookie1 = HTTP::Cookie.new("uid", "u12345", domain: 'example.org',
|
||||||
for_domain: true,
|
for_domain: true,
|
||||||
path: '/',
|
path: '/',
|
||||||
max_age: 7*86400)
|
max_age: 7*86400)
|
||||||
|
|
||||||
|
# Add it to the Set-Cookie response header
|
||||||
|
header['Set-Cookie'] = cookie1.set_cookie_value
|
||||||
|
|
||||||
# Generate a host-only cookie
|
# Generate a host-only cookie
|
||||||
cookie1 = HTTP::Cookie.new("aid", "a12345", origin: my_url,
|
cookie2 = HTTP::Cookie.new("aid", "a12345", origin: my_url,
|
||||||
path: '/',
|
path: '/',
|
||||||
max_age: 7*86400)
|
max_age: 7*86400)
|
||||||
|
|
||||||
# Set the Set-Cookie response header value
|
# Add it to the Set-Cookie response header
|
||||||
header['Set-Cookie'] = [
|
header['Set-Cookie'] = cookie2.set_cookie_value
|
||||||
cookie1.set_cookie_value,
|
|
||||||
cookie2.set_cookie_value,
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
## Incompatibilities with Mechanize::Cookie/CookieJar
|
## Incompatibilities with Mechanize::Cookie/CookieJar
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue