Browse Source

Merge pull request #53 from floatdrop/query-opt

Add query option
http2
Sindre Sorhus 10 years ago
parent
commit
9bc36b0286
  1. 5
      index.js
  2. 6
      readme.md
  3. 16
      test/test-http.js

5
index.js

@ -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');
@ -92,6 +93,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;

6
readme.md

@ -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`

16
test/test-http.js

@ -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();
@ -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();

Loading…
Cancel
Save