Recently viewed X

Response Cookie handling

Last updated October 20, 2017

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 beresp or resp depending on what part of the request you're in - so either

setcookie.get_value_by_name(beresp,"name")

or

setcookie.get_value_by_name(resp,"name")

as appropriate, replacing "name" with whatever the name of the value is. So for example, given this HTTP response from an origin