Lua
Lua
Example: Http.SetCookieXml method
Demonstrates theSetCookieXml method.
Chilkat Lua Downloads
local chilkat = require("chilkat")
local http = chilkat.Http.new()
http.save_cookies = true
http.cookie_dir = "c:/temp/cookie_cache"
-- For this initial request, we are not sending cookies.
-- We are starting a new (cookie) session and saving the cookies we receive.
http.send_cookies = false
-- Do a request that establishes and saves cookies in files in the cookie directory.
local html = http:quick_get_str("https://google.com/")
-- Examine the LastResponseHeader to see the cookies that were received:
print(http.last_response_header)
print("----------------------------------")
-- We can see the following Set-Cookie response header fields:
-- Set-Cookie: AEC=AVh_V2h-fsL2-****; expires=Mon, 23-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
-- Set-Cookie: NID=525=XC_cL****Hn7-WONX; expires=Thu, 26-Feb-2026 19:15:10 GMT; path=/; domain=.google.com; HttpOnly
-- The following file was created: c:/temp/cookie_cache/google_com.xml
-- Examine the cookies received in the above request.
print(http:get_cookie_xml("google.com"))
print("----------------------------------")
-- Sample output.
-- <?xml version="1.0" encoding="utf-8"?>
-- <cookies>
-- <cookie key=".google.com,/,AEC" v="0" expire="Mon, 23-Feb-2026 19:05:20 GMT" secure="yes">
-- <AEC>AVh_V2gL8QzTdFGYq6_rS6ktBfqm8WNG3pzHxS2nTZD5i23dRBau2c4ZRA</AEC>
-- </cookie>
-- <cookie key=".google.com,/,NID" v="0" expire="Thu, 26-Feb-2026 19:05:20 GMT">
-- <NID>525=SuLcnaSkFqF4Jz_jLEq4kt_f3MY2Xro1VDoVzLKvp8XHcW2UHuLKJSr55iDeW0NiRIPXoAwJWF1-YNl29unX2xfhEWsS5BhbuK_2DXdD9cTOmn5BSENMhZasxeJ71mEP2PQRMXBndqnl41DhblC2jjdac_so4TESIll1B0GCVe9wRFjqI6DTZItRCj61BHmr1_RAQi0_jrh_ihn6KYtIFEY7</NID>
-- </cookie>
-- </cookies>
-- -------------------------------------------------------------------------------------------
-- Let's say we want to continue with the session at some later time with a new HTTP object.
-- One way to do it is to simply set the same cookie properties:
local httpB = chilkat.Http.new()
httpB.save_cookies = true
httpB.cookie_dir = "c:/temp/cookie_cache"
httpB.send_cookies = true
-- The cookies from the cache are sent.
html = httpB:quick_get_str("https://google.com/")
-- Examine the LastHeader to see the contents of the Cookie header field.
print(httpB.last_header)
print("----------------------------------")
-- -------------------------------------------------------------------------------------------
-- Another way to do it is to explicitly load the cookies from XML
-- and set the cookies for the particular domain.
local httpC = chilkat.Http.new()
httpC.save_cookies = true
-- Do not save cookies to files, just keep them in memory.
httpC.cookie_dir = "memory"
httpC.send_cookies = true
local sbCookiesXml = chilkat.StringBuilder.new()
local success = sbCookiesXml:load_file("c:/temp/cookie_cache/google_com.xml", "utf-8")
if not success then
print(sbCookiesXml.last_error_text)
return
end
success = httpC:set_cookie_xml("google.com", sbCookiesXml:get_as_string())
if not success then
print(httpC.last_error_text)
return
end
html = httpC:quick_get_str("https://google.com/")
-- Examine the LastHeader to see the contents of the Cookie header field.
print(httpC.last_header)