Skip to content

Instantly share code, notes, and snippets.

@tuor4eg
Created June 25, 2018 13:40
Show Gist options
  • Save tuor4eg/9a3941aa3c1e4ecd80226fd97493a46b to your computer and use it in GitHub Desktop.
Save tuor4eg/9a3941aa3c1e4ecd80226fd97493a46b to your computer and use it in GitHub Desktop.
export default (queryPar) => {
const postData = querystring.stringify(queryPar.data);
const { protocol, hostname, pathname, port, query } = url.parse(queryPar.url, true);
const options = {
protocol,
host: hostname,
path: `${pathname}${getSearch(query, queryPar.params)}`,
port,
method: queryPar.method,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
},
};
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
const body = [];
res.on('data', chunk => {
body.push(chunk.toString());
}).on('end', () => {
const html = body.join('');
const getData = {
status: res.statusCode,
data: html,
}
resolve(getData);
});
});
req.on('error', err => {
reject(err);
});
if (postData) {
req.write(postData);
}
req.end();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment