CMS Development

TGerhard
Participant

Serverless Functions: Pass along cookies to client

Hi there, I'm using HubSpot Serverless functions and I have a simple use-case where I try to pass along all cookies I recieve from my API to my Client.

 

So basically I use the standard approach (like described in the docs) and it looks something like this:

exports.main = (context, sendResponse) => {
    axios.post("https://myapi.com", someDataToPass).then(function response(res) {
        sendResponse({
            body: response.data,
            statusCode: 200,
            headers: {
                "Set-Cookie": response.headers['set-cookie']
            }
        });
    });
};

 

Everything works well, except my cookies aren't  passed along to the client. I've also tried to replace "Set-Cookie" with other params like "Cookie" or "Cookies" in case any of that work but no success.

 

Surprisingly, if I change "Set-Cookie" to  "Location" the data is submitted to the client like it should. "Location" is also the only parameter that is documented here: https://developers.hubspot.com/docs/cms/features/serverless-functions/reference#redirect-by-sending-.... There is no further reference for other "headers" parameters like "Set-Cookie" for instance so I assume they just don't work with hubspot?

 

Does anyone have more insights about this?

0 Upvotes
4 Replies 4
TGerhard
Participant

Serverless Functions: Pass along cookies to client

 

Hi @joviedo- wow it seems to work now!

Only one more question. I'm actually trying to send multiple cookies - not just one. According to http specs (https://developer.mozilla.org/de/docs/Web/HTTP/Headers/Set-Cookie) "multiple Set-Cookie headers should be sent in the same response". But if I try to do that, only one of the Set-Cookie Headers arrives:

sendResponse({
          body: response.datastatusCode: 200headers: {
                        "Set-Cookie": "layout=v2; expires=Thu, 08-Jul-2021 06:04:31 GMT; Max-Age=604800; path=/",
                        "Set-Cookie": "apiKey=myKey; expires=Thu, 08-Jul-2021 06:04:31 GMT; Max-Age=604800; path=/"
                      }
        });
Chrome recieves only one Set-Cookie header (the second one):
response.png
 
Is this functionality also not implemented yet?
joviedo
HubSpot Product Team
HubSpot Product Team

Serverless Functions: Pass along cookies to client

Hi @TGerhard Set-Cookie was not supported as a response header but it was added recently. Could you try again with Set-Cookie?

Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Serverless Functions: Pass along cookies to client

Hey @TGerhard 

 

I believe location is strickly for a permenant redirects. Have you tried using the cookie header?

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
TGerhard
Participant

Serverless Functions: Pass along cookies to client

Hi @Kevin-C

yes, I have tried "Cookie" instead of "Location" and also "Set-Cookie" like in my example above but when I checked the response (in chrome dev tools) it seems like the cookie / Set-cookie Header was removed or rather never sent to the client at all. When I tried with "Location" the header was submitted and i was able to se it in the developer tools.

0 Upvotes