DataScript Avi HTTP Get Cookie
DataScript
Function | avi.http.get_cookie( name [, context] ) |
Description | Returns the value of a specific cookie from the client request or server response header. |
Events | HTTP_REQ HTTP_REQ_BODY HTTP_RESP |
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:
|
Example | Check the value of cookie named ‘foo’. If the value is not ‘bar’, then do something.
|
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
else
output = output .. ", " .. k .. ": " .. tostring(v)
end
k, v = next(attr_tbl, k)
end
if v then
output = output .. ", " .. k .. ": " .. v
end
avi.vs.log("value= " .. value .. "; attributes= " .. output)
If the response has this header:
Set-Cookie: A1=foobar; path=/path/to/file/; Domain=avinetworks.com; secure; HTTPOnly,
then the DS log will be:
"value= foobar; attributes= , path: /path/to/file/, Domain: avinetworks.com, 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)
end
if v then
output = output .. ", " .. k .. "=" .. v
end
avi.vs.log("cookies: " .. output)
DS log would be of the format: "cookies: , name1=value1, name2=value=2, name3=value3, ..."