Skip to content

Instantly share code, notes, and snippets.

@SamuelMarks
Last active April 2, 2016 01:37
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 SamuelMarks/d561575d13499c53ebead97e0a51eb04 to your computer and use it in GitHub Desktop.
Save SamuelMarks/d561575d13499c53ebead97e0a51eb04 to your computer and use it in GitHub Desktop.
"use strict";
var http_1 = require('http');
function httpF(method) {
return function (options, body_or_cb, cb) {
if (!cb) {
cb = body_or_cb;
body_or_cb = null;
}
options['method'] = method;
if (body_or_cb)
if (!options)
options = { 'headers': { 'Content-Length': Buffer.byteLength(body_or_cb) } };
else if (!options.headers)
options.headers = { 'Content-Length': Buffer.byteLength(body_or_cb) };
else if (!options.headers['Content-Length'])
options.headers['Content-Length'] = Buffer.byteLength(body_or_cb);
var req = http_1.request(options, function (res) {
if (!res)
return cb(res);
else if ((res.statusCode / 100 | 0) > 3)
return cb(res);
return cb(null, res);
});
body_or_cb && req.write(body_or_cb);
req.end();
return req;
};
}
var httpPOST = httpF('POST');
httpPOST({
protocol: 'http:',
host: 'httpbin.org',
path: '/post',
headers: { 'Content-Type': 'application/json' }
}, JSON.stringify({ hello: 'world' }), function (err, res) {
if (err)
console.error(err.statusCode, err.statusMessage);
else
console.info(res.statusCode, res.statusMessage);
});
import {RequestOptions, IncomingMessage, ClientRequest, request as http_request} from 'http';
interface callback {
(res: IncomingMessage): void;
}
interface cb {
(err: IncomingMessage, res?: IncomingMessage): void;
}
function httpF(method: 'POST'|'PUT'|'PATCH'|'HEAD'|'GET'|'DELETE') {
return (options: RequestOptions,
body_or_cb: string|callback|cb|AsyncResultCallback<{}>,
cb?: callback|cb|AsyncResultCallback<{}>): ClientRequest => {
if (!cb) {
cb = <callback|cb|AsyncResultCallback<{}>>body_or_cb;
body_or_cb = null;
}
options['method'] = method;
if (body_or_cb)
if (!options)
options = {'headers': {'Content-Length': Buffer.byteLength(<string>body_or_cb)}};
else if (!options.headers)
options.headers = {'Content-Length': Buffer.byteLength(<string>body_or_cb)};
else if (!options.headers['Content-Length'])
options.headers['Content-Length'] = Buffer.byteLength(<string>body_or_cb);
const req = http_request(options, (res: IncomingMessage) => {
if (!res) return (<cb>cb)(res);
else if ((res.statusCode / 100 | 0) > 3) return (<cb>cb)(res);
return (<cb>cb)(null, res);
});
//body_or_cb ? req.end(<string>body_or_cb, cb) : req.end();
body_or_cb && req.write(body_or_cb);
req.end();
return req;
}
}
const httpPOST = httpF('POST');
httpPOST({
protocol: 'http:',
host: 'httpbin.org',
path: '/post',
headers: {'Content-Type': 'application/json'}
}, JSON.stringify({hello: 'world'}), (err: IncomingMessage, res: IncomingMessage) => {
if (err) console.error(err.statusCode, err.statusMessage);
else console.info(res.statusCode, res.statusMessage);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment