diff --git a/index.js b/index.js index e086713..77fd46e 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,24 @@ var urlLib = require('url'); var http = require('http'); var https = require('https'); +var assign = require('object-assign'); -module.exports = function (url, cb) { +module.exports = function (url, opts, cb) { var redirectCount = 0; - var get = function (url, cb) { - var fn = urlLib.parse(url).protocol === 'https:' ? https : http; + var get = function (url, opts, cb) { + if (typeof opts === 'function') { + cb = opts; + opts = {}; + } - fn.get(url, function (res) { + cb = cb || function () {}; + + var parsedUrl = urlLib.parse(url); + var fn = parsedUrl.protocol === 'https:' ? https : http; + var arg = assign({}, parsedUrl, opts); + + fn.get(arg, function (res) { var ret = ''; // redirect @@ -43,5 +53,5 @@ module.exports = function (url, cb) { }).on('error', cb); }; - get(url, cb); + get(url, opts, cb); }; diff --git a/package.json b/package.json index 381d4b9..2e1cd80 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "got", "version": "0.1.1", - "description": "Simplified HTTP/HTTPS GET requests", + "description": "Simplified HTTP/HTTPS requests", "license": "MIT", "repository": "sindresorhus/got", "author": { @@ -30,6 +30,9 @@ "utility", "simple" ], + "dependencies": { + "object-assign": "^0.3.0" + }, "devDependencies": { "mocha": "*" } diff --git a/readme.md b/readme.md index 3e4d659..0f08c56 100644 --- a/readme.md +++ b/readme.md @@ -1,8 +1,8 @@ # got [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) -> Simplified HTTP/HTTPS GET requests +> Simplified HTTP/HTTPS requests -Follows redirects. Not intended to be feature-rich. Use [request](https://github.com/mikeal/request) if you need something more. +A nicer interface to the built-in [`http`](http://nodejs.org/api/http.html) module that also follows redirects. Use [request](https://github.com/mikeal/request) if you need more. ## Install @@ -24,6 +24,28 @@ got('http://todomvc.com', function (err, data) { ``` +### API + +It's a `GET` request by default, but can be changed in `options`. + +#### got(url, [options], [callback]) + +##### url + +*Required* +Type: `string` + +The url to request. + +##### options + +Type: `object` + +Any of the [`http.request`](http://nodejs.org/api/http.html#http_http_request_options_callback) options. + +##### callback(err, data) + + ## License [MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com) diff --git a/test.js b/test.js index 8cb816a..9b44eb8 100644 --- a/test.js +++ b/test.js @@ -2,7 +2,7 @@ var assert = require('assert'); var got = require('./index'); -it('should request', function (done) { +it('should do HTTP request', function (done) { got('http://google.com', function (err, data) { if (err) { console.error(err); @@ -13,7 +13,9 @@ it('should request', function (done) { assert(/google/.test(data)); done(); }); +}); +it('should do HTTPS request', function (done) { got('https://google.com', function (err, data) { if (err) { console.error(err); @@ -24,9 +26,20 @@ it('should request', function (done) { assert(/google/.test(data)); done(); }); +}); +it('should should return status code as error when not 200', function (done) { got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data) { assert.strictEqual(err, 404); done(); }); }); + +it('should support optional options', function (done) { + got('http://sindresorhus.com', {method: 'HEAD'}, function (err, data) { + assert(!err, err); + assert(!data, data); + done(); + }); +}); +