Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created August 6, 2020 16:19
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 JosePedroDias/f8c348c683185d67bbf2194185b9e1c1 to your computer and use it in GitHub Desktop.
Save JosePedroDias/f8c348c683185d67bbf2194185b9e1c1 to your computer and use it in GitHub Desktop.
to eventually use in node instead of request
const http = require('http');
const https = require('https');
const HEADERS = {};
function get(url, body, moreHeaders) {
const prot = url.indexOf('https:') === 0 ? https : http;
return new Promise((resolve, reject) => {
const r = prot.request(
url,
{
method: body ? 'POST' : 'GET',
headers: {
...HEADERS,
...(moreHeaders && moreHeaders),
},
},
(resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
try {
resolve(JSON.parse(data));
} catch (_) {
resolve(data);
}
});
resp.on('error', reject);
}
);
if (body) {
r.write(body);
}
r.end();
});
}
module.exports = { get };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment