Hi all.
I would like to change this python code in OutSystems. How can I do it?
# Create a session to handle cookiessession = requests.Session()# Prepare the payloadpayload = { "username": username, "password": password}# Send the POST requestresponse = 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)
@Kwan Yat MOK : You can use GetRequestHeader from HTTPRequestHandler to get all the cookies.
Thanks!!
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,
"Lax"
);
return true;
end;
#Subsequent API method checking the cookie
public function GetUserData(): UserData
cookies: Map of Text to Text;
#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");
#Return protected data
return GetCurrentUserData();