Skip to content

Instantly share code, notes, and snippets.

@fijimunkii
Created August 16, 2019 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fijimunkii/91e6926eeb61d319e0470a8866e21451 to your computer and use it in GitHub Desktop.
Save fijimunkii/91e6926eeb61d319e0470a8866e21451 to your computer and use it in GitHub Desktop.
node fetch get extract hash post
// grab page - extract text - send post request back to same page
const crypto = require('crypto');
const http = require('http');
const querystring = require('querystring');
const host = 'someapp.host';
const port = 38008;
const path = '/';
const options = { host, port, path };
let Cookie;
new Promise((resolve, reject) => {
const req = http.request(Object.assign(options, { method: 'GET' }), res => {
Cookie = res.headers['set-cookie'];
const data = [];
res.on('data', d => data.push(d));
res.on('end', () => resolve(Buffer.concat(data).toString()));
});
req.on('error', reject);
req.end();
})
.then(d => {
const token = /.*center'>([^<]+)<.*/g.exec(d)[1];
const hash = crypto.createHash('md5').update(token).digest('hex');
return new Promise((resolve, reject) => {
const postData = querystring.stringify({hash});
const headers = {
Cookie,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
};
const req = http.request(Object.assign(options, { method: 'POST', headers }), res => {
const data = [];
res.on('data', d => data.push(d));
res.on('end', () => resolve(Buffer.concat(data).toString()));
});
req.on('error', reject);
req.write(postData);
req.end();
});
})
.then(console.log, console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment