Monday, 29 May 2017

Node Request inside request with cookie

I am using node request var request = require(“request”); in my config node to do a POST request and in response get a Cookie which need to be referred in all rest of requests.

I tried enabling COOKIE JAR that works fine if i chain my request under first request but I want to call rest of requests like GetList from custom node.

I tried toughcookie (file cookie) not working when i add var j = request.jar(new FileCookieStore(‘cookies.json’)); node stop working with no error.

Below is my config node, code using which I am getting Cookie.

function myAuthNode(n) {

        RED.nodes.createNode(this,n);
        this.username=n.username;
        this.password=n.password;


            this.connect=function(){
                //TODO-NG need to make URL configurable
                request.post({url: "http://localhost:8080/api/method/login", qs: {usr: this.username, pwd: this.password}}, function(err, res, body) {

                    if(err) {
                            return console.error(err);
                    }

                    console.log("HERE IF I PUT REQUEST Works fine");
                    console.log("CAN WE PASS ANOTHER REQUEST here from calling SOURCE to execute here?");

                });

        };



    }

Here in this custom node I am calling

 // The main node definition - most things happen in here
    function GetListNode(n) {
        // Create a RED node
        RED.nodes.createNode(this,n);
        console.log('I am called');

        //using auth config now you are connected, tell me what is needed?
        this.authConfig=RED.nodes.getNode(n.auth);
        //connect to config and do auth

        this.authConfig.connect();

//THIS ALWAYS FAILS due to cookie not found where as I enable request JAR

       request.get({url: "http://localhost:8080/api/resource/Project"}, function(err, res, body) {

                if(err) {
                        return console.error(err);
                }

                    console.log("Response body:", body);

            });


    }

Please suggest how to handle cookie in request so that all requests after auth works fine?

Can we pass a request definition to another request for execution inside it or how Cookie can be handled ?



via Dev G

No comments:

Post a Comment