From 849fab7227a2fd1ff5fa4d603ba89037e1c462b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 26 May 2020 09:54:54 +0200 Subject: Cowboy 2.8.0 --- .../2.8/manual/cowboy_req.parse_cookies/index.html | 218 +++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 docs/en/cowboy/2.8/manual/cowboy_req.parse_cookies/index.html (limited to 'docs/en/cowboy/2.8/manual/cowboy_req.parse_cookies/index.html') diff --git a/docs/en/cowboy/2.8/manual/cowboy_req.parse_cookies/index.html b/docs/en/cowboy/2.8/manual/cowboy_req.parse_cookies/index.html new file mode 100644 index 00000000..a10b7eda --- /dev/null +++ b/docs/en/cowboy/2.8/manual/cowboy_req.parse_cookies/index.html @@ -0,0 +1,218 @@ + + + + + + + + + + Nine Nines: cowboy_req:parse_cookies(3) + + + + + + + + + + + + + + + + +
+
+
+
+ +

cowboy_req:parse_cookies(3)

+ +

Name

+

cowboy_req:parse_cookies - Parse cookie headers

+

Description

+
+
parse_cookies(Req) -> [{Name, Value}]
+
+Name  :: binary() %% case sensitive
+Value :: binary() %% case sensitive
+
+

Parse cookie headers.

+

Alias for cowboy_req:parse_header(<<"cookie">>, Req).

+

When the cookie header is missing or empty, [] is returned.

+

This function will crash on invalid cookie data. Because invalid cookies are fairly common when dealing with browsers (because of the string interface that the Javascript API provides), it is recommended to filter the cookie header value before attempting to parse it. This can be accomplished by calling the function cowboy_req:filter_cookies(3) first. This does not guarantee that parsing succeeds. If it still fails it is recommended to send an error response or redirect with instructions to delete the relevant cookies:

+
Recover from cookie parsing errors
+
+
Req1 = cowboy_req:filter_cookies([session_id, token], Req0),
+try cowboy_req:parse_cookies(Req1) of
+    Cookies ->
+        do_something(Req1, Cookies)
+catch _:_ ->
+    %% We can't parse the cookies we need, unset them
+    %% otherwise the browser will continue sending them.
+    Req2 = cowboy_req:set_resp_cookie(<<"session_id">>,
+        <<>>, Req1, #{max_age => 0}),
+    Req = cowboy_req:set_resp_cookie(<<"token">>,
+        <<>>, Req2, #{max_age => 0}),
+    cowboy_req:reply(500, Req)
+end.
+
+

Arguments

+
Req
+

The Req object.

+
+
+

Return value

+

The cookies are returned as a list of key/values. Keys and values are case sensitive binary strings.

+

Changelog

+
  • 2.0: Only the parsed header value is returned, it is no longer wrapped in a tuple. +
  • +
  • 2.0: Function introduced. Replaces cookie/2,3 and cookies/1. +
  • +
+

Examples

+
Look for a specific cookie
+
+
Cookies = cowboy_req:parse_cookies(Req),
+{_, Token} = lists:keyfind(<<"token">>, 1, Cookies).
+
+

See also

+

cowboy_req(3), cowboy_req:parse_header(3), cowboy_req:filter_cookies(3), cowboy_req:match_cookies(3)

+ + + + + + +
+ +
+ + +

+ Cowboy + 2.8 + Function Reference + +

+ + + +

Navigation

+ +

Version select

+ + +

Like my work? Donate!

+

Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and Erlang.mk is fantastic:

+
+ + + + + + + + + +

Recurring payment options are also available via GitHub Sponsors. These funds are used to cover the recurring expenses like food, dedicated servers or domain names.

+ + + +
+
+
+
+ + + + + + + + + -- cgit v1.2.3