Skip to content

Commit

Permalink
Provide ability to override content-type when json option used
Browse files Browse the repository at this point in the history
Fixes #784
  • Loading branch information
vvo committed Jan 24, 2014
1 parent 8d8f4f8 commit fe2f59f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -1033,12 +1033,15 @@ Request.prototype.json = function (val) {
if (typeof val === 'boolean') {
if (typeof this.body === 'object') {
this.body = safeStringify(this.body)
self.setHeader('content-type', 'application/json')
if (!self.hasHeader('content-type'))
self.setHeader('content-type', 'application/json')
}
} else {
this.body = safeStringify(val)
self.setHeader('content-type', 'application/json')
if (!self.hasHeader('content-type'))
self.setHeader('content-type', 'application/json')
}

return this
}
Request.prototype.getHeader = function (name, headers) {
Expand Down
12 changes: 12 additions & 0 deletions tests/test-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ s.listen(s.port, function () {
assert.equal(req.headers.cookie, 'foo=bar; quux=baz')
})

// Issue #784: override content-type when json is used
// https://github.com/mikeal/request/issues/784
createTest({
json: true,
method: 'POST',
headers: {'content-type': 'application/json; charset=UTF-8'},
body: {hello: 'my friend'}},function(req, res) {
assert.ok(req.headers['content-type']);
assert.equal(req.headers['content-type'], 'application/json; charset=UTF-8');
}
)

// There should be no cookie header when neither headers.cookie nor a cookie jar is specified
createTest({}, function (req, res) {
assert.ok(!req.headers.cookie)
Expand Down

0 comments on commit fe2f59f

Please sign in to comment.