Skip to content

Commit

Permalink
Add query option
Browse files Browse the repository at this point in the history
  • Loading branch information
floatdrop committed Apr 26, 2015
1 parent 9f14fa6 commit 53df1ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var https = require('https');
var urlLib = require('url');
var util = require('util');
var zlib = require('zlib');
var querystring = require('querystring');
var objectAssign = require('object-assign');
var infinityAgent = require('infinity-agent');
var duplexify = require('duplexify');
Expand Down Expand Up @@ -90,6 +91,10 @@ function got(url, opts, cb) {
}
}

if (opts.query) {
arg.path = (arg.path ? arg.path.split('?')[0] : '') + '?' + (typeof opts.query === 'string' ? opts.query : querystring.stringify(opts.query));
}

var req = fn.request(arg, function (response) {
var statusCode = response.statusCode;
var res = response;
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ _This option and stream mode are mutually exclusive._

If enabled, response body will be parsed with `JSON.parse`.

##### options.query

Type: `string`, `Object`

Query string object, that will be added to request url. This will override query string in `url`.

##### options.timeout

Type: `number`
Expand Down
16 changes: 16 additions & 0 deletions test/test-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ s.on('/404', function (req, res) {
}, 10);
});

s.on('/?recent=true', function (req, res) {
res.end('recent');
});

tape('setup', function (t) {
s.listen(s.port, function () {
t.end();
Expand Down Expand Up @@ -99,6 +103,18 @@ tape('timeout option', function (t) {
});
});

tape('query option', function (t) {
t.plan(2);

got(s.url, {query: {recent: true}}, function (err, data) {
t.equal(data, 'recent');
});

got(s.url, {query: 'recent=true'}, function (err, data) {
t.equal(data, 'recent');
});
});

tape('cleanup', function (t) {
s.close();
t.end();
Expand Down

0 comments on commit 53df1ca

Please sign in to comment.