Pull cookie values without regular expressions
Fastly shipped a pretty cool way to allow you to process cookies (and other headers) without using big ugly regular expressions.
From a example Cookie value of loggedin=true; serviceValue=xyz; testHash=as2d24g41
you can isolate specific values with the following syntax:
-
req.http.Cookie:loggedin
returns the stringtrue
-
req.http.Cookie:serviceValue
returns the stringxyz
-
req.http.Cookie:testHash
returns the stringas2d24g41
This will work with any header with multiple values, such as: Cookie, Set-Cookie, Cache-Control, or a custom header.
-
Supplying the cookie name via a table lookup doesn't seem to work though? Example:
req.http.Cookie:table.lookup(relaunch, "cookieName")
* fastly_service_v1.qa: [ERR] Invalid configuration for Fastly Service (5hYeq6hHppZbxco247CSKE): Syntax error: Expected ',' got '(' at: (input Line 253 Pos 35) req.http.Cookie:table.lookup(relaunch, "cookieName"), ----------------------------------#------------------------
-
Proper docs for this feature are here: https://docs.fastly.com/en/guides/isolating-header-values-without-regular-expressions
-
is it possible to get the value of a cookie, when the name of the cookie is a variable?
example:
declare local var.cookiename STRING;
set var.cookiename = "someValue"
req.http.Cookie:var.cookiename
I know it doesn't work this way, but how can I make this work when the cookie name is a variable?
-
You won't be able to refer to the cookie if the name is held in a variable. It has to be an actual string.
Do you not know the name of the cookie before runtime?
Please sign in to leave a comment.
Comments
8 comments