dev-resources.site
for different kinds of informations.
Elixir - Simple Req Cookie Jar
Published at
2/13/2024
Categories
elixir
scripting
cookies
Author
carlocolombo
Author
12 person written this
carlocolombo
open
A simple req cookie jar plugin. It can be used when automating or scraping websites that implement cookie based sessions. The cookies are set using set-cookie
header and are sent back to the server in the headers of each following request. The implementation is naive and probably misses advanced and corner cases.
# create the cookie jar
CookieJar.new()
# attach the plugin to the req
req = Req.new(base_url: "http://baseurl.example.com")
|> CookieJar.attach()
# when the response includes the set-cookie header, it store its value in the jar
%{body: "Ok."} = req
|> Req.post!(
url: "/api/v2/auth/login",
form: [username: "admin", password: "password"])
# all subsequent requests have the cookie set in the headers
%{status: 200} = req
|> Req.get!(url: "/api/v2/info?filter=foo")
The cookie is stored in an Agent. Req response and request steps are respectively used to collect the cookie string and to add to the cookie header when requests are performed.
defmodule ReqCookieJar do
use Agent
def new() do
Agent.start_link(fn -> "" end, name: __MODULE__)
end
defp get_cookie do
Agent.get(__MODULE__, & &1)
end
defp set_cookie([]), do: nil
defp set_cookie(val) do
Agent.update(__MODULE__, fn (_) -> val end)
end
def attach(%Req.Request{} = request) do
request
|> Req.Request.append_response_steps(
cookie_jar: fn({req, res})->
Req.Response.get_header(res, "set-cookie")
|> set_cookie()
{req,res}
end
)
|> Req.Request.append_request_steps(
cookie_jar: &Req.Request.put_header(&1, "cookie", get_cookie())
)
end
end
cookies Article's
30 articles in total
Cookies auto clearing after browser refresh issue , CORS related express cookies issue
read article
Understanding Cookies in Rails: A Developer's Guide
read article
Understanding JWT Tokens vs. Session Cookies: The Best for Web Authentication
read article
How to use Cookies in Postman?
read article
Comprehensive Guide to Cookies in JavaScript
read article
Understanding Cookies: What They Are, How They Work, and Why They Matter for Your Privacy
read article
What Cookies are Important for Privacy?
read article
Making the most annoying cookie banners we could think of 🍪🤬
read article
The Most Annoying Cookie Banner Ever Hackathon 🤬🍪
read article
How to Set Up User Cookies and Connect Google Analytics in Your React App
read article
Cookie Consent Headaches Across Subdomains
read article
Hacking Cookies and PWA on Ubuntu
read article
Third-Party Cookies Are Gone (Or Not). How Can I Still Embed Cross-Site Apps?
read article
🍪 Cookies - A humbling auth experience
read article
Javascript Ls/ss/cookies
read article
Contextual Targeting vs Cookies: Who will win in 2024?
read article
Javascript Ls/ss/cookies😎
read article
Introduction to JWT and Cookie storage
read article
Demystifying Session-Based Authentication: Your Angular Roadmap
read article
How to persist client-side preferences on the client in Svelte (w/o DB)
read article
Cookies in Depth using Javascript and NodeJs
read article
Cross-Domain Tracking Implementation
read article
Double chocolate chip cookie
read article
Mastering Authentication & Authorization: Exploring Identity Framework with .NET 8 and Migrations
read article
Elixir - Simple Req Cookie Jar
currently reading
Limitations of Cookies
read article
Cookies vs Session Storage vs Local Storage
read article
Best DX for cookies on localhost
read article
Understanding Cookies and Sessions in Node.js
read article
Cookies
read article
Featured ones: