Browse Source

Add accept: application/json header if json option is set

http2
Kirill Solovev 10 years ago
parent
commit
f94dcec259
  1. 4
      index.js
  2. 2
      readme.md
  3. 7
      test/test-headers.js

4
index.js

@ -55,6 +55,10 @@ function got(url, opts, cb) {
delete opts.timeout;
delete opts.query;
if (json) {
opts.headers.accept = opts.headers.accept || 'application/json';
}
if (body) {
opts.method = opts.method || 'POST';
}

2
readme.md

@ -76,7 +76,7 @@ Default: `false`
_This option and stream mode are mutually exclusive._
If enabled, response body will be parsed with `JSON.parse`.
If enabled, response body will be parsed with `JSON.parse` and `accept` header will be set to `application/json` by default.
###### query

7
test/test-headers.js

@ -32,6 +32,13 @@ tape('send accept-encoding header by default', function (t) {
});
});
tape('send accept header with json option', function (t) {
got(s.url, {json: true}, function (err, headers) {
t.equal(headers.accept, 'application/json');
t.end();
});
});
tape('send host header by default', function (t) {
got(s.url, function (err, data) {
var headers = JSON.parse(data);

Loading…
Cancel
Save