Below is my code which make http request to a web server having host 78.154.17.70, port 8080 and path /csrftoken.json.
I use request.get
to get the data from the server. I am able to fetch the data, which is Json data
in the following format:
{
"apiVersion": "1.0",
"data": {
"csrf": "ajbgajbgjabbjbjbab"
}
}
I declared two variables var CSRFTokenValue;
and var respJson;
globally. Inside request.get
, I use these variables as: respJson
store the parse data and CSRFTokenValue
store the
"csrf": "ajbgajbgjabbjbjbab" token value as "ajbgajbgjabbjbjbab"
means CSRFTokenValue
will store "ajbgajbgjabbjbjbab" in it.
Now when i log CSRFTokenValue
inside the request.get
it will give me the "ajbgajbgjabbjbjbab" but when I log it outside it will give me undefined .
I need CSRFTokenValue
to use in the quesryString inside the request.post
to post the data.
I don't know how to get the tokenValue globally??
var request = require('request');
var CSRFTokenValue;
var respJson;
request.get('http://78.154.17.70:8080/csrftoken.json', function (e, res, body){
respJson = JSON.parse(body);
CSRFTokenValue = respJson.data.csrf;
console.log('GET success:' + CSRFTokenValue);
});
console.log('GET token Globaly:' + CSRFTokenValue);
request.post('http://78.154.17.70:8080/Login/post.json?_csrf=' + CSRFTokenValue, {
'name' : 'name',
'password' : 'pass'
}, function (res) {
console.log('success');
});