Skip to content

Instantly share code, notes, and snippets.

@ejnshtein
Created March 11, 2019 15:23
Show Gist options
  • Save ejnshtein/d1c2a529e60ac9d4dbe5b456122e75ed to your computer and use it in GitHub Desktop.
Save ejnshtein/d1c2a529e60ac9d4dbe5b456122e75ed to your computer and use it in GitHub Desktop.
const ua = require('useragent-generator')
const querystring = require('querystring')
const https = require('https')
const userAgent = ua.chrome({
version: '72.0.3626.121',
os: 'Windows NT 10.0'
})
function post (url, formdata) {
const data = querystring.stringify(formdata)
return new Promise((resolve, reject) => {
const req = https.request(url, {
method: 'POST',
headers: {
'Content-Type': `application/x-www-form-urlencoded`,
'User-Agent': userAgent,
'Content-Length': Buffer.byteLength(data)
}
}, res => {
res.setEncoding('utf8')
let data = ''
res.on('data', (chunk) => {
data += chunk
})
res.on('error', err => {
reject(err)
})
res.on('close', () => {
resolve({
data,
headers: res.headers,
status: res.statusCode,
statusText: res.statusMessage
})
})
})
req.on('error', err => reject(err))
req.write(data, err => {
if (err) reject(err)
})
req.end()
})
}
module.exports = post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment