From f94dcec259585ef92928a55ec9f122b3721a7ab6 Mon Sep 17 00:00:00 2001 From: Kirill Solovev Date: Fri, 8 May 2015 16:33:16 +0300 Subject: [PATCH] Add accept: application/json header if json option is set --- index.js | 4 ++++ readme.md | 2 +- test/test-headers.js | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index aabdd74..8390926 100644 --- a/index.js +++ b/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'; } diff --git a/readme.md b/readme.md index 33c69d0..edad46e 100644 --- a/readme.md +++ b/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 diff --git a/test/test-headers.js b/test/test-headers.js index 6cf9ea9..5424b24 100644 --- a/test/test-headers.js +++ b/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);