Response Cookie handling
Last updated 2021-05-10
The traditional way to read response cookies in VCL is to inspect either the
beresp.http.Set-Cookie or the
resp.http.Set-Cookie variables and then extract values using regular expressions. However this is not ideal since attempting to parse potentially complicated or quoted strings with regular expressions is brittle and prone to being tripped up by edge cases. It also doesn't allow for reading multiple headers with the same name such as when an origin sends multiple
Set-Cookie headers. Because of these two reasons Fastly supports a method for extracting a named value out of
Set-Cookie headers no matter how many there are.
To access a named value simply use the function with either
resp depending on what part of the request you're in - so either
as appropriate, replacing
"name" with whatever the name of the value is. So for example, given this HTTP response from an origin
1HTTP/1.1 200 OK2Cache-Control: max-age=603Content-Type: text/html; charset=utf-84Content-Length: 808065Accept-Ranges: bytes6Date: Tue, 11 Aug 2015 19:00:04 GMT7Age: 1238Connection: keep-alive9Set-Cookie: one=a; httponly; secure10Set-Cookie: two=b or not to b; httponly
then using the function like this
set resp.http.X-One = setcookie.get_value_by_name(resp, "one");set resp.http.X-Two = setcookie.get_value_by_name(resp, "two");
resp.http.X-One to be "a" and
resp.http.X-Two to "b or not to b".
This logic can be used in uploaded custom VCL, as well as throughout the web interface. For example:
Do not use this form to send sensitive information. If you need assistance, contact support.