Skip to content

Commit

Permalink
Changing so if Accept header is explicitly set, sending json does not…
Browse files Browse the repository at this point in the history
… overwrite.

This change handles a case where the user has set the Accept header (other than application/json) for the request and wants to send JSON. Accept header is not overwritten.
A valid use case for this is a REST API where the API expects a custom Accept header for versioning reasons (i.e. application/vnd.custom.v1+json)
  • Loading branch information
RoryH committed Mar 15, 2013
1 parent d3e28ef commit 8f8bb9e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,15 +963,21 @@ Request.prototype.multipart = function (multipart) {
return self
}
Request.prototype.json = function (val) {
this.setHeader('accept', 'application/json')
var self = this;
var setAcceptHeader = function() {
if (!self.headers['accept'] && !self.headers['Accept']) {
self.setHeader('accept', 'application/json')
}
}
setAcceptHeader();
this._json = true
if (typeof val === 'boolean') {
if (typeof this.body === 'object') {
this.setHeader('content-type', 'application/json')
setAcceptHeader();
this.body = safeStringify(this.body)
}
} else {
this.setHeader('content-type', 'application/json')
setAcceptHeader();
this.body = safeStringify(val)
}
return this
Expand Down

0 comments on commit 8f8bb9e

Please sign in to comment.