Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.