Logging into a website once and reusing the session using Request - node.js
I'm using request.
I want to login a website only once, then use the same connection until it expires, so I won't have to login every time I send a request.
How can I do that?
I have tried the code below, but it doesn't retain the session. I'm using Redis to save cookie values, this is probably the wrong way, though. Two cookies are generated after you log in.
var request = require("request").defaults({jar: true, followAllRedirects: true});
var options1 = {
method: 'GET',
url: 'homepageURL',
headers:
{
'cache-control': 'no-cache',
'accept-language': 'tr,en-US;q=0.9,en;q=0.8,ru;q=0.7,it;q=0.6,fr;q=0.5,de;q=0.4,es;q=0.3,sv;q=0.2,nl;q=0.1,pl;q=0.1,pt;q=0.1,nb;q=0.1',
'accept-encoding': 'gzip, deflate, br',
referer: 'URL',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'upgrade-insecure-requests': '1'
}
};
request(options1, function (homeError, homeResponse, homeBody) {
if (homeResponse.statusCode == 200) {
var form = {"username": "myusername", "password": "mypassword", "secret": "mysecret", "action": "login"};
var options2 = {
method: 'POST',
url: 'siteurl + "/" + login.asp',
headers:
{
'cache-control': 'no-cache',
'accept-language': 'tr,en-US;q=0.9,en;q=0.8,ru;q=0.7,it;q=0.6,fr;q=0.5,de;q=0.4,es;q=0.3,sv;q=0.2,nl;q=0.1,pl;q=0.1,pt;q=0.1,nb;q=0.1',
'accept-encoding': 'gzip, deflate, br',
referer: 'URL',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
'upgrade-insecure-requests': '1',
origin: url + "/" + login.asp
},
form: form
};
request(options2, function (loginError, loginResponse, loginBody) {
if (loginResponse.statusCode == 200) {
console.log("Successfully logged in.");
client.set("cookie1", homeResponse.request.headers.cookie, function () {
client.set("cookie2", loginResponse.request.headers.cookie, function () {
nrp.emit('profile', {});
});
});
}
});
}
});
Profile test
nrp.on('profile', function () {
client.get("cookie1", function (cookie1Err, cookie1) {
client.get("cookie2", function (cookie2Err, cookie2) {
var cookie = cookie1 + ";" + cookie2;
var options = {
method: 'GET',
url: 'site/profile',
headers:
{
'cookie': cookie,
'accept-language': 'tr,en-US;q=0.9,en;q=0.8,ru;q=0.7,it;q=0.6,fr;q=0.5,de;q=0.4,es;q=0.3,sv;q=0.2,nl;q=0.1,pl;q=0.1,pt;q=0.1,nb;q=0.1',
'accept-encoding': 'gzip, deflate, br',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'upgrade-insecure-requests': '1'
}
};
request(options, function (error, response, body) {
console.log(body);
// it says you are not logged in, but if you use this inside the block above, it works.
});
})
});
});
Related
Unable to send authorisation token or some data in headers in axios
I am sending this from my react frontend to my node backend const data = await axios.post('http://localhost:5000/api/login', {email, password}, { headers:{ 'Authorization':'somesecretkey' } }); Where I am trying to accept headers like this const { headers } = req; console.log(headers); but i am getting this { host: 'localhost:5000', connection: 'keep-alive', 'content-length': '54', 'sec-ch-ua': '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"', accept: 'application/json, text/plain, */*', 'sec-ch-ua-mobile': '?0', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59', 'content-type': 'application/json;charset=UTF-8', origin: 'http://localhost:3000', 'sec-fetch-site': 'same-site', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', referer: 'http://localhost:3000/', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.9' } that is i am not getting any Authorisation header in backend. What i am doing wrong?
Gibrish when fetching an image and sending to client
In my server i read an image - jpg - from a url with : let _uri = "https.....xxxxxxxxxxx.jpg"; let _headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': '*', 'Accept-Language': 'en-US,en', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Referer': 'http://www.google.com/', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' } var options = { uri: _uri, headers: _headers }; rp(options) .then(parsedBody => { //response.setHeader('Content-Type', 'image/jpg'); // return response.status(200).send(parsedBody); //** don't work also return response.json(parsedBody); }) .catch(err => { response.status(400).send(err) }); }); No matter what i try, this will get a huge chunk of garbage when i check the response. I can't find out what type of data is it. On client i receive it - again - a lot of jibrish : fetch('https://...my server') .then(res => res.blob()) .then(blob => { console.log(blob); How do i get this photo on server and send to client ?
Add an option encoding: null to have the response returned as Buffer var options = { uri: _uri, headers: _headers, encoding: null // add this line } Then you can just send the buffer as response rp(options) .then(buffer => { response.setHeader('Content-Type', 'image/jpg'); return response.status(200).send(buffer); })
Request retuning possible gzip
I have this code: var options = { method: 'POST', url: '...', headers: { 'accept-language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7', 'accept-encoding': 'gzip, deflate, br', referer: '...', 'content-type': 'application/json;charset=UTF-8', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36', 'x-csrf-jwt': `${test}`, 'x-requested-with': 'XMLHttpRequest', origin: '...' }, body: `...` }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); }); But, the body is returning incomprehensibly. Look: E�IU�w��Msk�������%&�vj�Xĉ���0�J���Nxֈ��N�����J��{�g�������d�|��l�m�5��5�V�qs0��e�ٮ>VZE�`�`X�DX��Ed?9I�#��SO�C���u�D�<Dmn�O14WEr#�#�6���jY�9FN�p���H�Ox�7n�bt��.Vq�f�_#5�� ���jJU����K���8S��)3ns��:X�]G"~� Ϯ3�Q��r�g�I��9�Ar�#�q�Γ~XcoNj�}x/#u�R��g/����L���Vs(N��o\���N��:�F�u}��zf�'�dz�'њKB���h��ۻDg��\%5�i�O��)#q�T<9��qT�~v�B�4Ň��|�^�k��M�� �� #IYE This only happens with node.js Was it returning compressed in gzip? If so, how can I decompress it?
Add gzip: true to options and remove accept-encoding from options.headers
set Content-Type to json but request comes in as x-www-form-urlencoded
I am using ajax to send post to my app. In postman everything works as expected, however in browser, data comes in as x-www-form-urlencoded and data is missing. Below is my ajax and postman configurations and their corresponding headers in req... $.ajax({ url: '/', headers: { 'Accept': 'application/json', 'Content-Type': "application/json" }, // send as JSON data: JSON.stringify(formData), method: 'POST' }); headers: { host: 'localhost:8080', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0', accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'accept-language': 'en-US,en;q=0.5', 'accept-encoding': 'gzip, deflate', referer: 'http://localhost:8080/', 'content-type': 'application/x-www-form-urlencoded', 'content-length': '0', connection: 'keep-alive', 'upgrade-insecure-requests': '1', pragma: 'no-cache', 'cache-control': 'no-cache' } headers: { data: '{"name":"tester", "email": "t#t.com", "message":"This is a test"}', contenttype: 'application/json', datatype: 'xml/html/script/json', 'cache-control': 'no-cache', 'postman-token': 'cbaa5cbe-0ab8-4328-abfd-f5dc6a6acd90', 'user-agent': 'PostmanRuntime/7.1.1', accept: '*/*', host: 'localhost:8080', 'accept-encoding': 'gzip, deflate', 'content-length': '0', connection: 'keep-alive' } also my relevant server code: app.use(bodyParser.json()); //handle urlencoded data app.use(bodyParser.urlencoded({extended:false})); app.post('/', function (req, res) { console.log(res); console.log('----------------------------------------------------------'); console.log(req); //var jsonData = JSON.parse(req.headers.data); console.log(req.body); console.log(req.data); let transporter = courier.createTransport({...}); let mailOptions = {...}; transporter.sendMail(mailOptions, function (error, info) { if (error) console.log(error); else console.log('email sent:' + info.response); }); res.render('index', { title: 'Projects', projects: projects }); }); app.get('/', function (req, res) { res.render('index', { title: 'Projects', projects: projects }); }); plz halp!
on you can just simply: JSON.parse(data) inside your success: function(data){ var object =JSON.parse(data); } once the json parses the data you can do what ever you like.
Http request in nodejs with form data and query string
How can I send both query string parameters and form data parameters in nodejs. I tried the following code. but it's not working. it is just hanging and no response is comming. var url_req = require("request"); url_req({ pool: separateReqPool, url: "https://somthing.com/v1/message/userF", method: 'POST', qs: { action: "send-message", c: "somecode_c", cache: new Date().getTime(), sid: "somecode_sid" }, form: { convId: "userF~userT~esp", message: "<font face='Arial' size='10'>.</font>", sendAs: "userF", txnId: "userF~" + new Date().getTime() + "~number", }, headers: { 'Content-Type': 'application/json;charset=utf-8', 'Cookie': 'some cookies', 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0', 'Accept-Language': "en-US,en;q=0.5", 'Connection': "keep-alive", 'Pragma': "no-cache", 'Cache-Control': "no-cache", 'Referer': "https://something.com/xframe.html?bc", 'Accept': ' text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Content-Length': 100, 'Host': "something.com" } }, function (error, response, body) { if (error) console.log(error); else console.log(body); });