# Problem with cookie extraction

**URL:** <https://discourse.nodered.org/t/problem-with-cookie-extraction/60685>\
**Category:** General\
**Created:** [2 April 2022 13:45 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685 "2022-04-02T13:45:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 13:45 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/1 "2022-04-02T13:45:10Z")

</div>

Hello,

I am not a permanent user of NODE-RED, I am more using CURL and JQ ...

NOW, I have a small project where the LOGIN of a webserver is returning 3 cookies named  
JSESSION ID  
CatchMe  
SID-TOKEN  
.  
In the flows, using a function block, I can get to the cookie values e.g. with

var myCookie1= msg.responseCookies.CatchMe; \<=== is working

var myCookie2= msg.responseCookies.SID-TOKEN; \<=== is not working, producing an error message

It works well except with the cookie SID-TOKEN, where the function block is interpreting the cookie-name as a formula, missing TOKEN then and throws an error message.

Here, I am struggling with a solution. I have the _feeling_ that the problem can be solved within the function-block or with a change block, but I am, too blind to see it ...

Any help is welcome 🙂 ...  
df5zx

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 April 2022 13:50 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/2 "2022-04-02T13:50:49Z")

</div>

> [@df5zx](#):
>
> var myCookie2= msg.responseCookies.SID-TOKEN;

It will fail on the '-' character , could you try:

```auto
var myCookie2= msg.responseCookies["SID-TOKEN"]; 

```

---

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 13:58 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/3 "2022-04-02T13:58:43Z")

</div>

> [@bakman2](#):
>
> `msg.responseCookies["SID-TOKEN"];`

That I tried already without success ... The error changes from

_"ReferenceError: TOKEN is not defined (line 3, col 17)"_

to

_"SyntaxError: Unexpected token '[' (body:line 3)"_

My idea was to change the stupid cookie name from SID-TOKEN to SID\_TOKEN in the message from the http-in, but failed to figure out how to do that ...

---

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 14:06 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/4 "2022-04-02T14:06:29Z")

</div>

I of course could send the msg through exec to a shell, do the manipulation there, and collecting the changed msg back, but that's quite a workaround 🙂 ...

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 April 2022 14:15 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/5 "2022-04-02T14:15:36Z")

</div>

Can you copy the output from a debug node before the function node and paste the output here (use the \</\> icon to get proper formatting)

---

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 15:35 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/6 "2022-04-02T15:35:19Z")

</div>

Here is a complete message ... I tried also some JSONata code, but did not succeed either ... If I only knew how to convert the whole msg into a simple string, work on that in a function and then put it back into a JSON msg ...

```auto
{"_msgid":"b64d8fdf250a30f9","headers":{"set-cookie":["JSESSIONID=EE9E66DF34F03307A3C85ADDC20F64C5; Path=/; HttpOnly","CatchMe=deleteMe; Path=/; Max-Age=0; Expires=Fri, 01-Apr-2022 15:28:08 GMT","SID-TOKEN=3d50bc1d-93f6-4bed-b748-7d2472fa4bb1; Path=/"],"x-sid-token":"3d50bc1d-93f6-4bed-b748-7d2472fa4bb1","content-length":"0","date":"Sat, 02 Apr 2022 15:28:08 GMT","connection":"close","x-node-red-request-node":"29f62d64"},"method":"POST","url":"http://127.0.0.1:8099/login.jsp","payload":"","statusCode":200,"responseUrl":"http://127.0.0.1:8099/login.jsp","redirectList":[],"retry":0,"responseCookies":{"JSESSIONID":{"Path":"/","value":"EE9E66DF34F03307A3C85ADDC20F64C5"},"CatchMe":{"Path":"/","Max-Age":"0","Expires":"Fri, 01-Apr-2022 15:28:08 GMT","value":"deleteMe"},"SID-TOKEN":{"Path":"/","value":"3d50bc1d-93f6-4bed-b748-7d2472fa4bb1"}}}

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 April 2022 15:54 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/7 "2022-04-02T15:54:29Z")

</div>

It works for me. Can you post the full code from the function node ?

---

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 15:59 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/8 "2022-04-02T15:59:43Z")

</div>

Hmm, some strange then ... here is the filter function code for testing:

```auto
var cookies=msg.responseCookies;
sid=cookies.SID-TOKEN;
return sid;

```

---

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 16:07 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/9 "2022-04-02T16:07:15Z")

</div>

As you can see, I was playing a little with different code, the initial  
var myCookie2= msg.responseCookies.SID-TOKEN;  
is not working either.  
If I am not able to extract that cookie for further usage, then it is not worth to continue with NODE-RED and I will better go back to curl and pq in scripting ...

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 April 2022 16:12 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/10 "2022-04-02T16:12:49Z")

</div>

You need to return `msg` as object, and assign a new property ie:

```auto
const msg.sid = msg.responseCookies["SID-TOKEN"]
return msg;

```

The msg object is the core of a node-red flow.

---

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 16:34 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/11 "2022-04-02T16:34:04Z")

</div>

Got for that

_"SyntaxError: Identifier 'msg' has already been declared (body:line 1)"_

but I got the idea as well, and now

var cookies=msg.responseCookies;  
msg.responseCookies.sid=cookies["SID-TOKEN"];  
return msg;

does the job. I can read the cookie by that (and for testing storing under sid as well in a copy)

However, this code

```auto
msg.responseCookies.sid=msg.responseCookies["SID-TOKEN"];
return msg;

```

does it as well ... I had read such kind of copying of an object, but is was always throwing some error, when I tested ... strange

Anyway, thanks for your help, problem is solved 😃 🫱 🫱 🫱

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 April 2022 18:41 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/12 "2022-04-02T18:41:09Z")

</div>

Note that there is quite [extensive documentation available for node-red](https://nodered.org/docs/user-guide/messages). As a beginner I would recommend to use as little function nodes as possible, many things can be done with the change and switch nodes.

---

<div class="post-metadata">

**Author:** ![df5zx](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@df5zx](https://discourse.nodered.org/u/df5zx)\
**Post date:** [2 April 2022 19:14 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/13 "2022-04-02T19:14:03Z")

</div>

Yes, there is a lot of docs and also some books, which are laying here beside me. This is the frst trial under Windows of NODE-RED at me and with http... I did more than a handful of flows under small Linux boxes mainly with typical PLC protocols like MODBUS, Siemens S7 and OPC/UA along with some Arduino type micros or older legacy serial comms HVAC equipment. That was never a problem...  
This time, I need to run it on Windows, because there is a Window-only application what needs to be connected to PLC systems and where no interface is included or even supported...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 June 2022 19:14 UTC](https://discourse.nodered.org/t/problem-with-cookie-extraction/60685/14 "2022-06-01T19:14:49Z")

</div>

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