3

This question relates to: Is it possible to add http headers. for example set-cookie, via ampscript or ssjs in ExactTarget?

The answer on that question does not specifically call out how to set a cookie with SSJS, it just references the HTTPHeader.SetValue() function.

I've tried the following to set the cookie and have been unsuccessful.

HTTPHeader.SetValue("Set-Cookie","foo=bar");
HTTPHeader.SetValue("Cookie","foo=bar");

Any thoughts as to what I'm doing wrong?

2
  • Did you searched for and tried the standard methods of setting a cookie? en.wikipedia.org/wiki/HTTP_cookie#Setting_a_cookie Jun 10, 2016 at 4:21
  • 1
    The link you provided shows the following example to set a session cookie (which is what I'm trying to do): Set-Cookie: theme=light I'm still missing how I'm doing it wrong in the context of SSJS. Can you enlighten me and provide example code that should work?
    – Daniel
    Jun 11, 2016 at 14:53

1 Answer 1

3

There is a SetCookie value you can use in SSJS.

<script runat=server>
 Platform.Response.SetCookie("cookieName","cookieValue","2015-12-31 140000.999",true);

Properties are below. Values 3 and 4 (the date and time and HTTPS connection) are optional.

SetCookie(Session cookie name, Session cookie value, Date and time value indicating cookie expiration, Indicates whether cookie works with only HTTPS connections)

Note that there is no way to set persistent cookies via this function and it only sets it as a session cookie (deletes the cookie after the browser is closed).

3
  • 2
    adding to Jackson's answer: the datetime string can be in this format: 5/1/2019 12:12:06. You can create that with the default getHours(), getFullYear(),... methods. SFMC will auto-convert that to GMT inside of the SetCookie() method! A working method to create the datetime string was provided by Adam here: salesforce.stackexchange.com/a/145821/28940 May 1, 2019 at 17:59
  • @jackson Chen I just tested this and it seems I was able to close my browser and come back and the cookie was still there. What am I missing? Jun 1, 2022 at 22:04
  • In fact I cannot even seem to delete my cookie with RemoveCookie Jun 2, 2022 at 18:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .