Created
August 16, 2019 15:55
-
-
Save fijimunkii/91e6926eeb61d319e0470a8866e21451 to your computer and use it in GitHub Desktop.
node fetch get extract hash post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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