Http utilities
Convert 'get' function into a Promise.
const fetch = require('nyks/http/fetch');
(async function() {
await fetch('http://endpoint.com/services'); // return readable stream or throw
})();
Call http/https.get, depending on the enpoint Url.
const url = require('url');
const get = require('nyks/http/get');
get(url.parse('http://endpoint.com/services'), function(res) {
// res can be drained
});
// works with https endpoint too
get(url.parse('https://endpoint.com/services'), function(res) {
// res can be drained
});
// works with url as string too
get('http://endpoint.com/services', function(res) {
// res can be drained
});
Helper for http/https.request.
const request = require('nyks/http/request');
let target = 'http://endpoint.com/services'; // you can also use an parsed Url
let data = {
name : 'Jean Lebon'
};
await res = request(target, data);
// res can be drained
// you need to check for the statusCode yourself, or pass {expect : 200} in the target
/*
You have multiple options here :
* You can pass GET arguments in Url.
* Passing an jar entry in parsed Url Object will create cookies from this jar.
* Target.method can be forced.
* you can passe query string in parsed Url Object, it will write GET parametters automatically (for ex : target.qs = {name : 'Jean'}).
*/
Return an Object of parsed headers (or cookies).