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?

  • Did you searched for and tried the standard methods of setting a cookie? en.wikipedia.org/wiki/HTTP_cookie#Setting_a_cookie – David Murphy Jun 10 '16 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 '16 at 14:53
  • Please select as accepted answer if I have answered your question. – Jackson Chen May 2 '19 at 17:26
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).

  • 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 – Jörn Berkefeld May 1 '19 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.