Get cookies in server

Hi. I'm struggling with cookies. I can set and read it back with the UI template Node. But this is running in the browser. Is it possible to get the cookie in a server node for example in a function node or template node? I have try a lot things, Http request.... But i can get the cookie. Anybody know how i can do it?

Hi @jaap

can you provide some more details about what you are trying to do? You mention the UI template node - so is this to do with cookies in requests to the dashboard?

If you can access the cookies in the template node, then you could have the template node send a message back to Node-RED with the cookies in.

Or is this about HTTP requests sent to the HTTP In node? In which case you can access cookies using msg.req.cookies.

I use this in a UI template. I wil get the cookie and use it in a payload for another node

<!DOCTYPE html>
<html>
    
<head>
<script>

checkCookie()

function setCookie(cname,cvalue,exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires=" + d.toGMTString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i < ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

function checkCookie() {
  let user = getCookie("username");
  if (user != "") {
        document.getElementById("myText").innerHTML = ("Welkom terug " + user);
    //alert("Welcome again " + user);
  } else {
     //user = user_input[0];
     user = prompt("Wat is uw naam:","");
     if (user != "" && user != null) {
       setCookie("username", user, 1);
     }
  }
}
</script>
</head>
<h4> <span id="myText"></span></h4>
</html>


This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.