Help needed for https POST / cookie

Hi all

I would like to read data out of a water softener (BWT Aqua Perla) which has a webinterface in my own LAN. Therefore its needed as a first step to login through POST request to gain the cookie. The cookie is from CAKEPHP. Then a second call to another site on the webapp should return a json string. Nevertheless, I‘m facing the issue, i get the cookie, transfer it to second http request, but it returns a 302 error, indicating that a url redirect happens. msg. payload relay as well on login page...

do you have any ideas, how I can change the second call to get the json information?

The way should be as this:

Login:
Post Request to http://xx.xx.xx.xx/users/login with Content "_method=POST&STLoginPWField=xxxxxx&function=s ave"

—> Response-Cookie "CAKEPHP"

Getting the data with url:
/home/actualizedata

—>response
{"aktuellerDurchfluss":"0","aktuellerDurchflussProzent":"0","durchflussHeute":"0","durchflussMonat":"0","durchflussJahr":"0","RegeneriemittelNachfuellenIn":"0","RegeneriemittelVerbleibend":"0"}

This is the code for the nodes.

[{"id":"da556b4c.191f68","type":"tab","label":"BWT Perla Seta","disabled":false,"info":""},{"id":"7d4a8e14.598fd","type":"debug","z":"da556b4c.191f68","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"responseUrl","targetType":"msg","statusVal":"","statusType":"auto","x":1170,"y":240,"wires":[]},{"id":"718329ef.af2aa","type":"inject","z":"da556b4c.191f68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":340,"wires":[["7b705cf5.8d7644"]]},{"id":"fc22ddec.3cea4","type":"debug","z":"da556b4c.191f68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"responseCookies","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":160,"wires":[]},{"id":"7b705cf5.8d7644","type":"function","z":"da556b4c.191f68","name":"","func":"msg.headers = \n{ \"Content-Type\" : \"application/x-www-form-urlencoded\" ,\n\"Origin\": \"https://192.168.1.189\",\n\"Host\": \"192.168.1.189\",\n\"Referer\": \"https://192.168.1.189/users/login\",\n\"Upgrade-Insecure-Requests\": \"1\",\n\"STLoginPWField\": \"dzfuqv\",\n\"function\":\"save\"}\nmsg.rejectUnauthorized = false;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":240,"wires":[["52fd04b6.7857ac"]]},{"id":"52fd04b6.7857ac","type":"http request","z":"da556b4c.191f68","name":"login","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://192.168.1.189/","tls":"","persist":false,"proxy":"","authType":"","x":450,"y":240,"wires":[["fc22ddec.3cea4","b5f435fd.62b8"]]},{"id":"b5f435fd.62b8","type":"function","z":"da556b4c.191f68","name":"","func":"msg.headers = {\n    \"Host\": \"192.168.1.189\",\n    \"Upgrade-Insecure-Requests\": 1,\n    \"STLoginPWField\": \"dzfuqv\",\n    \"function\":\"save\"\n};\nmsg.cookies = msg.responseCookies;\nmsg.rejectUnauthorized = false;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":735.3333129882812,"y":240.33334350585938,"wires":[["c330713d.f7513","7a461eae.0028c"]]},{"id":"c330713d.f7513","type":"http request","z":"da556b4c.191f68","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://192.168.1.189/home/actualizedata/","tls":"","persist":false,"proxy":"","authType":"","x":950,"y":240,"wires":[["7d4a8e14.598fd","9621282.0604158","853b6de9.7ecd5"]]},{"id":"7a461eae.0028c","type":"debug","z":"da556b4c.191f68","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"cookies","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":400,"wires":[]},{"id":"9621282.0604158","type":"debug","z":"da556b4c.191f68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1160,"y":380,"wires":[]},{"id":"853b6de9.7ecd5","type":"debug","z":"da556b4c.191f68","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"cookies","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":180,"wires":[]}]

Thanks a lot for your assist
Thomas

I am reading from mobile so I haven’t imported your flow but read the function node. Can you explain what you’re trying to accomplish by putting the payload you’re posting

In the headers, rather than in msg.payload?
I think the issue of the redirect might be that the session cookie that is returned to you might be for a session that failed to log in.

hi afelix

thanks for your prompt answer. The first try was to use standard login from request node, but, the webapp has not a valid ssl certificate (given from BWT) so i had to do a workaround. I traced the login procedure with firefox to see the steps in between as curl, and googled how to to that with nodered..

after login i get the cookie "CAKEPHP"
and should send this cookie with the next request to: ../home/actualizedata

to catch the json.

I will rewrite the node with your idea to payload, but still no luck.

msg.headers = 
{ "Content-Type" : "application/x-www-form-urlencoded" ,
"Origin": "https://192.168.1.189",
"Host": "192.168.1.189",
"Referer": "https://192.168.1.189/users/login",
"Upgrade-Insecure-Requests": "1"
}
msg.payload = {
    "STLoginPWField": "dzfuqv",
    "function":"save"}
msg.rejectUnauthorized = false;
return msg;

Thanks a lot
Thomas

I incorporated the curl POST script here as well. Probably that helps.

curl "https://192.168.1.189/users/login" 
-H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0" 
-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" 
-H "Accept-Language: de,en-US;q=0.7,en;q=0.3" 
--compressed 
-H "Content-Type: application/x-www-form-urlencoded" 
-H "Origin: https://192.168.1.189" 
-H "Connection: keep-alive" 
-H "Referer: https://192.168.1.189/users/login" 
-H "Cookie: CAKEPHP=977fde0f224f2bf83db5e47a02303b71" 
-H "Upgrade-Insecure-Requests: 1" 
--data-raw "_method=POST&STLoginPWField=dzfuqv&function=save"

Hi

filling msg.payload does the trick finally. See code below.

[{"id":"56efe5dc.c6ddac","type":"debug","z":"da556b4c.191f68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":340,"wires":[]}]

Thanks a lot
Best regards
Thomas

In case someone finds this topic later on when looking for the same thing, they might be disappointed to find out your code snippet only contains a single debug node. :wink:

2 Likes

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