HTTP GET and POST Request with same Session ID ?

2,601 views
Skip to first unread message

Kay Pohl

unread,
May 28, 2017, 4:42:01 PM5/28/17
to Node-RED
Hello together,

im trying to use a rest api service which is running on an nginx server on a raspberry pi. Therefore i must first login to the site bevore i can do a rest call. In my browser it works great, also with chrome extension postman. Then i tried it with node red without luck. Here is my node.



[{"id":"26bab905.7b6886","type":"comment","z":"f8c76b6e.7d9bc8","name":"GET and POST","info":"","x":110,"y":40,"wires":[]},{"id":"17b87124.b78bdf","type":"http request","z":"f8c76b6e.7d9bc8","name":"","method":"use","ret":"obj","url":"","tls":"","x":530,"y":120,"wires":[["148c676f.d62d89","b53d4941.5e5408"]]},{"id":"93905d57.277b8","type":"inject","z":"f8c76b6e.7d9bc8","name":"Logged In?","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":150,"y":120,"wires":[["6e76a238.0c386c"]]},{"id":"c719c310.3a695","type":"inject","z":"f8c76b6e.7d9bc8","name":"Login","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":130,"y":180,"wires":[["95a4e1b9.bfa54"]]},{"id":"148c676f.d62d89","type":"debug","z":"f8c76b6e.7d9bc8","name":"","active":true,"console":"false","complete":"false","x":730,"y":120,"wires":[]},{"id":"6e76a238.0c386c","type":"function","z":"f8c76b6e.7d9bc8","name":"Get Payload","func":"msg.url = \"http://10.23.59.254/api/auth/loggedin\";\nmsg.headers = {\"user-agent\": \"node-red\"};\nmsg.method = \"GET\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":120,"wires":[["17b87124.b78bdf"]]},{"id":"95a4e1b9.bfa54","type":"function","z":"f8c76b6e.7d9bc8","name":"POST Payload","func":"msg.url = \"http://10.23.59.254/api/auth/login\";\nmsg.headers = {\"user-agent\": \"node-red\"};\nmsg.method = \"POST\";\nmsg.payload = {\"username\":\"XXX\",\"password\":\"XXX\"};\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":180,"wires":[["17b87124.b78bdf"]]},{"id":"b53d4941.5e5408","type":"debug","z":"f8c76b6e.7d9bc8","name":"","active":true,"console":"false","complete":"headers","x":730,"y":180,"wires":[]}]



As you can see in the Debug Output, first i perform a get request with the response "logged in:false". Thats ok. Then i send a post request with username and password. Response is a json object with username, encrypted password and an ID. After that, i send the get request again, but response is again "logged in:false". That´s not ok. If i do this requests in the chrome extension postman with the same sequence, then i get the following response "logged in:true". I can´t see any difference between Node Red requests and postman requests. Therefore i think it could be a session problem? Does anyone have experience with session id´s in httprequests?

Kind Regards
Kay
 

Julian Knight

unread,
May 28, 2017, 5:00:49 PM5/28/17
to Node-RED
You need to know what the API server is expecting. Is it using basic auth, cookie based auth or JWT or something else?

Kay Pohl

unread,
May 28, 2017, 6:31:38 PM5/28/17
to Node-RED
It seems to be a cookie based auth:

Glenn

unread,
May 28, 2017, 9:20:30 PM5/28/17
to Node-RED
I currently do what you want.

When I get home I can explain it better. Do not override anything otherwise it will get confused.

Use msg.url to pass the url and msg.method to pass whether it is get or post.

Provide headers in msg.payload in json format.


Hope it helps

Kay Pohl

unread,
May 29, 2017, 2:46:30 AM5/29/17
to Node-RED
Hi Glenn,

Use msg.url to pass the url and msg.method to pass whether it is get or post.

Provide headers in msg.payload in json format.


I do this already. What i am figured out is that i am must using the cookie i get from the POST command as response to feed the GET request. How can i do this in a function?

Kay

Julian Knight

unread,
May 29, 2017, 4:31:51 AM5/29/17
to Node-RED
The cookie should be in the response data you get when you login. msg.res I think. It is a rather large object though so you might want to look it up rather than experiment as that might take a while. 

Nick O'Leary

unread,
May 29, 2017, 4:36:45 AM5/29/17
to Node-RED

msg.res is the response object provided by the HTTP In node - not the HTTP Request node.

Kay, the cookie provided by the first request will be under msg.headers - if you pass that to a debug node you'll see it has a property called 'cookies'. You should pass that property through to the next http request node... I think that would do it.

In the last release we made cookie handling easier with the HTTP In/Response node pair. We should look at doing the same with the Http request node.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/968c154f-b4d5-4e8e-aadd-e9e9e51b0a0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages