DataScript: avi.http.update_cookie

DataScript

Function avi.http.update_cookie( [context] )
Description Modify the existing cookie attributes value in the HTTP response.
If the specified cookie("name") exists
  • If cookie attribute/attributes already present in an existing cookie, then the function will modify the mentioned cookie attribute/attributes to "attribute_value".
  • If cookie attribute/attributes does not exist, function will add the mentioned cookie attribute/attributes to the cookie.
If the cookie does not exist, do nothing.
Events HTTP_RESP
Parameter name: The name of the cookie to be modified.
attr: attribute of an cookie("name") which has to be modified/added.
attr_val: value to be replaced with existing cookie attribute value.
Attributes:
path is the value (a string) of the path attribute
domain is the value (a string) of the domain attribute
expires is the value (an integer) of the expires attribute
maxage is the value (an integer) of the maxage attribute
httponly is the value (a boolean) of the httponly attribute
secure is the value (a boolean) of the secure attribute
samesite is the value (an integer) of the samesite attribute samesite attribute values : avi.http.Lax avi.http.None avi.http.Strict
Returns None
Example 1 - Modifying single Set-Cookie attribute: Example of server sending a simple cookie with the name ‘name1’ and path as ‘/dummy’:

server cookie example :  Set-Cookie: name1=value1; path=/dummy
If you want to modify server cookie path to /dummy1 Data Script to modify the path attribute:

avi.http.update_cookie("name1","path","/dummy1")
Set-Cookie will be modified to

Set-Cookie: name1=value1; Path=/dummy1
Example 2 - Adding and modifying Set-cookie attribute: Example of server sending a simple cookie with the name ‘name2’ and path as ‘/dummy’:

server cookie example:  Set-Cookie: name2=value2; path=/dummy
If you want to update this server cookie with Domain to ‘www.avinetworks2.com’, HttpOnly and modify the path to ‘/dummy2’ with expiring in next 30 mins. Example of Data Script to add and modify cookie attributes:

cookie_name="name2"
if avi.http.cookie_exists(cookie_name) then
 avi.http.update_cookie(cookie_name, "expires", os.time() + 1800 , "path", "/dummy2", "domain", "www.avinetworks2.com", "httponly", true)
end

Set-cookie will be modified to :

Set-Cookie: name2=value2; Path=/dummy2; Domain=www.avinetworks2.com; HttpOnly

Notes:

  1. avi.http.update_cookie() API doesn’t have any order for attributes.
  2. Max-Age cookie attribute can be modified/updated using avi.http.update_cookie().
  3. avi.http.update_cookie() API can be used to add individual/multiple attributes to an existing cookie.
  4. If avi.http.update_cookie() is called for the same cookie name multiple times, it can result in creating multiple Set-Cookie headers for the same cookie name. So, if multiple attributes for the same cookie need to be specified, please use the API to update multiple attributes in a single call as shown above.
  5. expires calculates from epoch start time 1st January, 1970. So if the user wants it to expire in 1 hr from the current time, they should use os.time + 3600 (as the units are in seconds).