DataScript Avi HTTP Get Cookie


Function avi.http.get_cookie( name [, context] )
Description Returns the value of a specific cookie from the client request or server response header.
Parameter Name: The name of the cookie to inspect
Context: Rather than inspect the cookie from the current event context, specify the context of either avi.HTTP_REQUEST or avi.HTTP_RESPONSEto inspect cookies from those events.
For instance, during an HTTP response event, check for a cookie sent by the client during the request event.
Note: You cannot provide avi.HTTP_RESPONSE as the context argument to get_cookie if calling the function in HTTP_REQ or HTTP_REQ_BODY events)
Return Value If name is provided:
  • String of the cookie’s value if it exists, else nil.
  • (Only in HTTP_RESP && didn't provide avi.HTTP_REQUEST as context arg): Table of attributes provided in the Set-Cookie header as they appear in the response. Key: attribute name, Value: Attribute Value
    • If
      Set-Cookie: A1=foobar; path=/path/to/file/;; secure; HTTPOnly
      , then the table is:
    • {path = '/path/to/file/', Domain = '', secure = true, HTTPOnly = true}
If name is not provided:
  • Table of all cookies provided in the Request or Response headers. Key: Cookie Name, Value: Cookie Value
    • Example:
      {name1 = 'value1', name2 = 'value2', name3 = 'value3'}
Example Check the value of cookie named ‘foo’. If the value is not ‘bar’, then do something.
if avi.http.get_cookie("foo") ~= "bar" then
   -- do something with the cookie header

Additional Examples

Example 1: get_cookie in the HTTP_RESP event and showing the attributes table

value, attr_tbl = avi.vs.get_cookie("A1")

local k, v = next(attr_tbl, nil) 
output = ""
while k do 
	if type(v) ~= "boolean" then 
		output = output .. ", " .. k .. ": " .. v 
		output = output .. ", " .. k .. ": " .. tostring(v) 
	k, v = next(attr_tbl, k) 
if v then 
	output = output .. ", " .. k .. ": " .. v 

avi.vs.log("value= " .. value .. "; attributes= " .. output)

If the response has this header:

Set-Cookie: A1=foobar; path=/path/to/file/;; secure; HTTPOnly,

then the DS log will be:

"value= foobar; attributes= , path: /path/to/file/, Domain:, secure: true, HTTPOnly: true"

Example 2: get_cookie w/o the name argument

cookies = avi.vs.get_cookie()

local k, v = next(cookies, nil) 
output = ""
while k do 
	output = output .. ", " .. k .. "=" .. v 
	k, v = next(attr_tbl, k) 

if v then 
	output = output .. ", " .. k .. "=" .. v 

avi.vs.log("cookies: " .. output)

DS log would be of the format: "cookies: , name1=value1, name2=value=2, name3=value3, ..."