30
Views
3
Comments
Solved
Get Cookies in API
Question

Hi all. 

I would like to change this python code in OutSystems. How can I do it?

# Create a session to handle cookies
session = requests.Session()
# Prepare the payload
payload = {    
         "username": username,    
         "password": password
}
# Send the POST request
response = session.post(url, data=payload)
cookies=''
for i in session.cookies:    
       if len(cookies) == 0:
             cookies+=f"{i.name}={i.value}".strip()    
      else:
             cookies+="; " + f"{i.name}={i.value}".strip()
print("Cookies:" + cookies)

2019-01-07 16-04-16
Siya
 
MVP
Solution

@Kwan Yat MOK : You can use GetRequestHeader from HTTPRequestHandler to get all the cookies.



UserImage.jpg
Kwan Yat MOK

Thanks!!

2025-03-12 07-08-15
Nilesh Trivedi

Hi @Kwan Yat MOK,

OutSystems REST APIs cookies handling.

public function Authenticate(username: Text, password: Text): Boolean

var

    authToken: Text;

begin

    #Validate credentials

    if not User_Login(username, password) then

        #Clear any existing auth cookie on failed login

        SetResponseCookie("authToken", "", Now() - 1, "/");

        return false;

    end if;

    

    #Generate and set new auth token cookie

    authToken = GenerateSecureToken();

    SetResponseCookie(

        "authToken",

        authToken,

        Now() + 8 * 60 * 60, // 8 hour expiration

        "/api",

        "",

        true,

        true,

        "Lax"

    );

    

    return true;

end;


#Subsequent API method checking the cookie

public function GetUserData(): UserData

var

    cookies: Map of Text to Text;

begin

    #Get and parse cookies

    cookies = ParseCookies(GetRequestHeader("Cookie"));

    

   #Verify auth token

    if not cookies.ContainsKey("authToken") 

       or not ValidateToken(cookies["authToken"]) then

        RaiseException(401, "Unauthorized");

    end if;

    

   #Return protected data

    return GetCurrentUserData();

end;

2019-01-07 16-04-16
Siya
 
MVP
Solution

@Kwan Yat MOK : You can use GetRequestHeader from HTTPRequestHandler to get all the cookies.



UserImage.jpg
Kwan Yat MOK

Thanks!!

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.