Skip to content

Commit

Permalink
Merge pull request #1785 from simov/revert-json
Browse files Browse the repository at this point in the history
Revert: Set default application/json content-type when using json option #1772
  • Loading branch information
simov committed Sep 25, 2015
2 parents 9b37c85 + f6b91b5 commit 833c6ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
9 changes: 6 additions & 3 deletions request.js
Expand Up @@ -1198,19 +1198,22 @@ Request.prototype.json = function (val) {
}

self._json = true
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
}
if (typeof val === 'boolean') {
if (self.body !== undefined) {
if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
self.body = safeStringify(self.body)
} else {
self.body = self._qs.rfc3986(self.body)
}
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
}
}
} else {
self.body = safeStringify(val)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
}
}

if (typeof self.jsonReviver === 'function') {
Expand Down
5 changes: 2 additions & 3 deletions tests/test-defaults.js
Expand Up @@ -73,7 +73,6 @@ tape('deep extend', function(t) {
}, function (e, r, b) {
delete b.headers.host
delete b.headers.accept
delete b.headers['content-type']
delete b.headers.connection
t.deepEqual(b.headers, { a: '1', b: '3', c: '4' })
t.deepEqual(b.qs, { a: '1', b: '3', c: '4' })
Expand All @@ -98,7 +97,7 @@ tape('post(string, object, function)', function(t) {
}).post(s.url + '/', { json: true }, function (e, r, b) {
t.equal(b.method, 'POST')
t.equal(b.headers.foo, 'bar')
t.equal(b.headers['content-type'], 'application/json')
t.equal(b.headers['content-type'], undefined)
t.end()
})
})
Expand All @@ -109,7 +108,7 @@ tape('patch(string, object, function)', function(t) {
}).patch(s.url + '/', { json: true }, function (e, r, b) {
t.equal(b.method, 'PATCH')
t.equal(b.headers.foo, 'bar')
t.equal(b.headers['content-type'], 'application/json')
t.equal(b.headers['content-type'], undefined)
t.end()
})
})
Expand Down
10 changes: 10 additions & 0 deletions tests/test-json-request.js
Expand Up @@ -72,6 +72,16 @@ testJSONValueReviver('jsonReviver', -48269.592, function (k, v) {
}, 48269.592)
testJSONValueReviver('jsonReviverInvalid', -48269.592, 'invalid reviver', -48269.592)

tape('missing body', function (t) {
s.on('/missing-body', function (req, res) {
t.equal(req.headers['content-type'], undefined)
res.end()
})
request({url:s.url + '/missing-body', json:true}, function () {
t.end()
})
})

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

0 comments on commit 833c6ef

Please sign in to comment.