Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit. Request package.

  • Loading branch information...
commit 758f598de8d6024db3fa8ee7d0a1fc3e45c50f53 0 parents
@mikeal mikeal authored
34 README.md
@@ -0,0 +1,34 @@
+# Request -- Simplified HTTP request method
+
+### Install
+
+<pre>
+ npm install request
+</pre>
+
+### Super simple to use
+
+request(options, callback);
+
+<pre><code>
+ var request = require('request');
+ request({uri:'http://www.google.com'}, function (error, response, body) {
+ if (!error && response.statusCode == 200) {
+ sys.puts(body) // Print the google web page.
+ }
+ })
+</code></pre>
+
+### Options
+
+The first argument is an options object. The only required option is uri, all others are optional.
+
+uri : fully qualified uri or a parsed url object from url.parse()
+method : http method, defaults to GET
+headers : http headers, defaults to {}
+body : entity body for POST and PUT requests
+client : existing http client object (when undefined a new one will be created and assigned to this property so you can keep around a reference to it if you would like use keep-alive on later request)
+
+### callback
+
+The callback argument gets 3 arguments. The first is an error when applicable (usually from the http.Client option not the http.ClientRequest object). The second in an http.ClientResponse object. The third is the response body buffer.
120 lib/base64.js
@@ -0,0 +1,120 @@
+/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
+ * Version: 1.0
+ * LastModified: Dec 25 1999
+ * This library is free. You can redistribute it and/or modify it.
+ */
+
+ /* Modified by Dan Webb not to require Narwhal's binary library */
+
+var encodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+var decodeChars = [
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
+ -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
+ -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
+];
+
+exports.encode = function (str) {
+ var out, i, length;
+ var c1, c2, c3;
+
+ length = len(str);
+ i = 0;
+ out = [];
+ while(i < length) {
+ c1 = str.charCodeAt(i++) & 0xff;
+ if(i == length)
+ {
+ out.push(encodeChars.charCodeAt(c1 >> 2));
+ out.push(encodeChars.charCodeAt((c1 & 0x3) << 4));
+ out.push("=".charCodeAt(0));
+ out.push("=".charCodeAt(0));
+ break;
+ }
+ c2 = str.charCodeAt(i++);
+ if(i == length)
+ {
+ out.push(encodeChars.charCodeAt(c1 >> 2));
+ out.push(encodeChars.charCodeAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)));
+ out.push(encodeChars.charCodeAt((c2 & 0xF) << 2));
+ out.push("=".charCodeAt(0));
+ break;
+ }
+ c3 = str.charCodeAt(i++);
+ out.push(encodeChars.charCodeAt(c1 >> 2));
+ out.push(encodeChars.charCodeAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)));
+ out.push(encodeChars.charCodeAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)));
+ out.push(encodeChars.charCodeAt(c3 & 0x3F));
+ }
+
+ var str = "";
+ out.forEach(function(chr) { str += String.fromCharCode(chr) });
+ return str;
+};
+
+exports.decode = function (str) {
+ var c1, c2, c3, c4;
+ var i, length, out;
+
+ length = len(str);
+ i = 0;
+ out = [];
+ while(i < length) {
+ /* c1 */
+ do {
+ c1 = decodeChars[str.charCodeAt(i++) & 0xff];
+ } while(i < length && c1 == -1);
+ if(c1 == -1)
+ break;
+
+ /* c2 */
+ do {
+ c2 = decodeChars[str.charCodeAt(i++) & 0xff];
+ } while(i < length && c2 == -1);
+ if(c2 == -1)
+ break;
+
+ out.push(String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)));
+
+ /* c3 */
+ do {
+ c3 = str.charCodeAt(i++) & 0xff;
+ if(c3 == 61)
+ return out.join('');
+ c3 = decodeChars[c3];
+ } while(i < length && c3 == -1);
+ if(c3 == -1)
+ break;
+
+ out.push(String.fromCharCode(((c2 & 0xF) << 4) | ((c3 & 0x3C) >> 2)));
+
+ /* c4 */
+ do {
+ c4 = str.charCodeAt(i++) & 0xff;
+ if(c4 == 61)
+ return out.join('');
+ c4 = decodeChars[c4];
+ } while(i < length && c4 == -1);
+
+ if(c4 == -1)
+ break;
+
+ out.push(String.fromCharCode(((c3 & 0x03) << 6) | c4));
+ }
+
+ return out.join('');
+};
+
+var len = function (object) {
+ if (object.length !== undefined) {
+ return object.length;
+ } else if (object.getLength !== undefined) {
+ return object.getLength();
+ } else {
+ return undefined;
+ }
+};
60 lib/main.js
@@ -0,0 +1,60 @@
+var http = require('http'),
+ url = require('url'),
+ sys = require('sys'),
+ base64 = require('./base64');
+
+function request (options, callback) {
+ if (!options.uri) {
+ throw new Error("options.uri is a required argument")
+ } else {
+ if (typeof options.uri == "string") {
+ options.uri = url.parse(options.uri);
+ }
+ }
+
+ options.method = options.method ? options.method : 'GET';
+ options.headers = options.headers ? options.headers : {};
+ if (!options.headers.host) {
+ options.headers.host = options.uri.hostname;
+ if (options.uri.port) {
+ options.headers.host += (':'+options.uri.port)
+ }
+ }
+ if (!options.uri.pathname) {options.uri.pathname = '/'}
+ if (!options.uri.port) {
+ if (options.uri.protocol == 'http:') {options.uri.port = 80}
+ else if (options.uri.protocol == 'https:') {options.uri.port = 443}
+ }
+
+ var clientErrorHandler = function (error) {
+ callback(error);
+ }
+ options.client = options.client ? options.client : http.createClient(options.uri.port, options.uri.hostname);
+ options.client.addListener('error', clientErrorHandler);
+
+ if (options.uri.auth && !options.headers.authorization) {
+ options.headers.authorization = "Basic " + base64.encode(options.uri.auth);
+ }
+ options.pathname = options.uri.search ? (options.uri.pathname + options.uri.search) : options.uri.pathname;
+ if (options.body) {options.headers['content-length'] = options.body.length}
+
+ options.request = options.client.request(options.method, options.pathname, options.headers);
+
+ options.request.addListener("response", function (response) {
+ var buffer = '';
+ response.addListener("data", function (chunk) {
+ buffer += chunk;
+ })
+ response.addListener("end", function () {
+ options.client.removeListener("error", clientErrorHandler);
+ callback(null, response, buffer);
+ })
+ })
+
+ if (options.body) {
+ options.request.write(options.body, 'binary');
+ }
+ options.request.end();
+}
+
+module.exports = request;
16 package.json
@@ -0,0 +1,16 @@
+{ "name" : "request"
+, "description" : "Simplified HTTP request method."
+, "tags" : ["http", "simple", "util", "utility"]
+, "version" : "0.6.0"
+, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>"
+, "directories" :
+ { "lib" : "lib" }
+, "repository" :
+ { "type" : "git"
+ , "url" : "http://github.com/mikeal/node-utils.git"
+ }
+, "bugs" :
+ { "web" : "http://github.com/mikeal/node-utils/issues" }
+, "engines" : ["node >=0.1.90"]
+, "main" : "./lib/main"
+}
29 tests/couch.js
@@ -0,0 +1,29 @@
+var request = require('../lib/main'),
+ sys = require('sys');
+
+function testports (port) {
+ var uri = port ? 'http://mikeal.couchone.com' + ":" + port : 'http://mikeal.couchone.com';
+ sys.puts(uri)
+ request({uri:uri}, function (error, response, body) {
+ if (error) {throw new Error(error)};
+ if (!response.statusCode == 200) {
+ throw new Error("Wrong status code "+response.statusCode);
+ }
+ var v = '{"couchdb":"Welcome",'
+ if (body.slice(0, v.length) !== v) {
+ throw new Error("Unexpected body\n"+body);
+ }
+ })
+}
+testports();
+testports(80)
+testports(5984)
+
+var randomnumber=Math.floor(Math.random()*11).toString();
+request({uri:'http://mikeal.couchone.com/testjs', method:'POST', body:'{"_id":"'+randomnumber+'"}'},
+ function (error, response, body) {
+ if (error) {throw new Error(error)};
+ if (!response.statusCode == 201) {
+ throw new Error("Wrong status code "+response.statusCode);
+ }
+ });
Please sign in to comment.
Something went wrong with that request. Please try again.