Skip to content

Latest commit

 

History

History
90 lines (62 loc) · 1.77 KB

http.md

File metadata and controls

90 lines (62 loc) · 1.77 KB

Http

Http utilities


Table of Contents


fetch(url) : Promise

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
})();

get(url[, callback]) : void

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
});

request(url[, data]) : void

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'}).
*/

header/parse(str) : Object

Return an Object of parsed headers (or cookies).