Skip to content

Commit

Permalink
fix: set content-length: 0 for put/patch requests with empty body
Browse files Browse the repository at this point in the history
fixes #570
  • Loading branch information
gr2m committed Sep 30, 2017
1 parent e45869f commit 54391c7
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,7 @@ var Client = module.exports = function(config) {
if (config.pathPrefix && url.indexOf(config.pathPrefix) !== 0) {
url = config.pathPrefix + def.url;
}
var ret = {
query: format == "json" ? {} : format == "raw" ? msg.data : []
};
var ret = {};
if (!def || !def.params) {
ret.url = url;
return ret;
Expand Down Expand Up @@ -613,8 +611,14 @@ var Client = module.exports = function(config) {
if (format == "json" && def.params[paramName].sendValueAsBody) {
ret.query = val;
} else if (format == "json") {
if (!ret.query) {
ret.query = {}
}
ret.query[paramName] = val;
} else if (format != "raw") {
if (!ret.query) {
ret.query = []
}
ret.query.push(paramName + "=" + val);
}
}
Expand Down Expand Up @@ -657,7 +661,7 @@ var Client = module.exports = function(config) {
var query = obj.query;
var url = this.config.url ? this.config.url + obj.url : obj.url;
var path = url;
if (!hasBody && query.length) {
if (!hasBody && query && query.length) {
path += "?" + query.join("&");
}

Expand All @@ -684,7 +688,7 @@ var Client = module.exports = function(config) {
} else if (format != "raw") {
query = query.join("&");
}
headers["content-length"] = Buffer.byteLength(query, "utf8");
headers["content-length"] = Buffer.byteLength(query || '', "utf8");
headers["content-type"] = format == "json"
? "application/json; charset=utf-8"
: format == "raw"
Expand Down Expand Up @@ -828,7 +832,7 @@ var Client = module.exports = function(config) {
});

// write data to request body
if (hasBody && query.length) {
if (hasBody && query && query.length) {
if (self.debug) {
console.log("REQUEST BODY: " + query + "\n");
}
Expand Down

0 comments on commit 54391c7

Please sign in to comment.