Browse Source

add support for options

http2
Sindre Sorhus 11 years ago
parent
commit
21121688cb
  1. 20
      index.js
  2. 5
      package.json
  3. 26
      readme.md
  4. 15
      test.js

20
index.js

@ -2,14 +2,24 @@
var urlLib = require('url'); var urlLib = require('url');
var http = require('http'); var http = require('http');
var https = require('https'); var https = require('https');
var assign = require('object-assign');
module.exports = function (url, cb) { module.exports = function (url, opts, cb) {
var redirectCount = 0; var redirectCount = 0;
var get = function (url, cb) { var get = function (url, opts, cb) {
var fn = urlLib.parse(url).protocol === 'https:' ? https : http; if (typeof opts === 'function') {
cb = opts;
opts = {};
}
cb = cb || function () {};
var parsedUrl = urlLib.parse(url);
var fn = parsedUrl.protocol === 'https:' ? https : http;
var arg = assign({}, parsedUrl, opts);
fn.get(url, function (res) { fn.get(arg, function (res) {
var ret = ''; var ret = '';
// redirect // redirect
@ -43,5 +53,5 @@ module.exports = function (url, cb) {
}).on('error', cb); }).on('error', cb);
}; };
get(url, cb); get(url, opts, cb);
}; };

5
package.json

@ -1,7 +1,7 @@
{ {
"name": "got", "name": "got",
"version": "0.1.1", "version": "0.1.1",
"description": "Simplified HTTP/HTTPS GET requests", "description": "Simplified HTTP/HTTPS requests",
"license": "MIT", "license": "MIT",
"repository": "sindresorhus/got", "repository": "sindresorhus/got",
"author": { "author": {
@ -30,6 +30,9 @@
"utility", "utility",
"simple" "simple"
], ],
"dependencies": {
"object-assign": "^0.3.0"
},
"devDependencies": { "devDependencies": {
"mocha": "*" "mocha": "*"
} }

26
readme.md

@ -1,8 +1,8 @@
# got [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) # 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 ## 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 ## License
[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com) [MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com)

15
test.js

@ -2,7 +2,7 @@
var assert = require('assert'); var assert = require('assert');
var got = require('./index'); var got = require('./index');
it('should request', function (done) { it('should do HTTP request', function (done) {
got('http://google.com', function (err, data) { got('http://google.com', function (err, data) {
if (err) { if (err) {
console.error(err); console.error(err);
@ -13,7 +13,9 @@ it('should request', function (done) {
assert(/google/.test(data)); assert(/google/.test(data));
done(); done();
}); });
});
it('should do HTTPS request', function (done) {
got('https://google.com', function (err, data) { got('https://google.com', function (err, data) {
if (err) { if (err) {
console.error(err); console.error(err);
@ -24,9 +26,20 @@ it('should request', function (done) {
assert(/google/.test(data)); assert(/google/.test(data));
done(); done();
}); });
});
it('should should return status code as error when not 200', function (done) {
got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data) { got('http://sindresorhus.com/sfsadfasdfadsga', function (err, data) {
assert.strictEqual(err, 404); assert.strictEqual(err, 404);
done(); done();
}); });
}); });
it('should support optional options', function (done) {
got('http://sindresorhus.com', {method: 'HEAD'}, function (err, data) {
assert(!err, err);
assert(!data, data);
done();
});
});

Loading…
Cancel
Save