Skip to content

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.9.0"}
  ]
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 = HttpCookie.Jar.put_cookies_from_headers(jar, url, received_headers)

# before making requests, prepare the cookie header
{:ok, cookie_header_value, jar} = HttpCookie.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)

Usage with Tesla

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

{:ok, server_pid} = HttpCookie.Jar.Server.start_link()
tesla = Tesla.client([{HttpCookie.TeslaMiddleware, jar_server: server_pid}])

Tesla.get!(tesla, "https://example.com/one")
Tesla.get!(tesla, "https://example.com/two")

About

Standards-compliant HTTP Cookie implementation for Elixir

Resources

License

Stars

Watchers

Forks

Contributors 2

  •  
  •  

Languages