Cookies

The FireflyAIO Lua API provides functions for managing cookies, which are essential for maintaining session information and user data during web requests. The two primary functions for cookie management are AddCookie and ClearCookies.

AddCookie

The AddCookie function is used to add a cookie to your request. This is useful for maintaining session information, user preferences, and other data that needs to persist between requests.

Usage

AddCookie(name, value, url_path, domain)
  • name: The name of the cookie.

  • value: The value of the cookie.

  • url_path: The path within the domain where the cookie is valid.

  • domain: The domain for which the cookie is valid.

Example

-- Add a cookie to the request
AddCookie("session_id", "abc123", "/", "example.com")

In this example, the cookie named "session_id" with the value "abc123" is added for the domain "example.com" and is valid for all paths ("/").

ClearCookies

The ClearCookies function is used to clear all cookies from your request. This is useful for resetting the session or ensuring that no previous cookies interfere with your current request.

Usage

ClearCookies()

Example

-- Clear all cookies from the request
ClearCookies()

By using ClearCookies(), you ensure that any previously set cookies are removed, providing a clean state for your next request.

Combining AddCookie and ClearCookies

You can use these functions together to manage cookies effectively in your custom checkers. For example, you might clear all cookies at the start of a checker and then add specific cookies required for the session.

Example

function Checker(username, password)
    -- Clear any existing cookies
    ClearCookies()

    -- Add necessary cookies for the request
    AddCookie("session_id", "abc123", "/", "example.com")
    AddCookie("token", "abc123", "/auth", "example.com")
    
    -- Proceed with your request logic
    local response = Post("https://example.com/api/login", "username=" .. username .. "&password=" .. password, "application/x-www-form-urlencoded")

    if string.find(response, "access_token") then
        return "Hit"
    elseif string.find(response, "invalid credentials") then
        return "Fail"
    else
        return "Retry"
    end
end

By effectively managing cookies with AddCookie and ClearCookies, you can ensure that your requests maintain the necessary session data and operate in a controlled environment.

Last updated