Problem with cookie extraction

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 :slight_smile: ...
df5zx

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

var myCookie2= 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 ...

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 :slight_smile: ...

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)

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 ...

{"_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"}}}

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

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

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

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 ...

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

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

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

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

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 :smiley: :rightwards_hand: :rightwards_hand: :rightwards_hand:

Note that there is quite extensive documentation available for node-red. 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.

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...

1 Like