Skip to content

Standards-compliant HTTP Cookie implementation for Elixir

License

Notifications You must be signed in to change notification settings

reisub/http_cookie

Repository files navigation

HttpCookie

CI License Version Hex Docs

RFC6265-compliant HTTP Cookie implementation for Elixir.

Installation

The package can be installed by adding http_cookie to your list of dependencies in mix.exs:

def deps do
  [
    {:http_cookie, "~> 0.7.0"},
    # not needed if the public suffix check is disabled,
    # but it's highly recommended leaving it enabled
    {:public_suffix, github: "axelson/publicsuffix-elixir"}
  ]
end

Usage

url = URI.parse("https://example.com")

# create a cookie jar
jar = HttpCookie.Jar.new()

# when a response is received, save any cookies that might have been returned
received_headers = [{"Set-Cookie", "foo=bar"}]
jar = Jar.put_cookies_from_headers(jar, url, received_headers)

# before making requests, prepare the cookie header
{:ok, cookie_header_value, jar} = Jar.get_cookie_header_value(jar, url)

Usage with Req

HttpCookie can be used with Req to automatically set and parse cookies in HTTP requests:

empty_jar = HttpCookie.Jar.new()

req =
  Req.new(base_url: "https://example.com", plug: plug)
  |> HttpCookie.ReqPlugin.attach()

%{private: %{cookie_jar: updated_jar}} = Req.get!(req, url: "/one", cookie_jar: empty_jar)
%{private: %{cookie_jar: updated_jar}} = Req.get!(req, url: "/two", cookie_jar: updated_jar)

About

Standards-compliant HTTP Cookie implementation for Elixir

Resources

License

Stars

Watchers

Forks

Languages