Skip to content

Commit a3735e0

Browse files
committed
Merge pull request #57 from yahoo/contentType
only add content-type header for PUT and POST requests
2 parents cc7999a + 016c669 commit a3735e0

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

libs/util/http.client.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,31 @@ if (!String.prototype.trim) {
3838
};
3939
}
4040

41-
function normalizeHeaders(headers) {
41+
function normalizeHeaders(headers, method) {
4242
var normalized = {};
43+
var needContentType = (method === METHOD_PUT || method === METHOD_POST);
4344
_.forEach(headers, function (v, field) {
4445
if (field.toLowerCase() === 'content-type') {
45-
normalized[CONTENT_TYPE] = v;
46+
if (needContentType) {
47+
normalized[CONTENT_TYPE] = v;
48+
}
4649
} else {
4750
normalized[field] = v;
4851
}
4952
});
53+
54+
if (needContentType && !normalized[CONTENT_TYPE]) {
55+
normalized[CONTENT_TYPE] = TYPE_JSON;
56+
}
57+
5058
return normalized;
5159
}
5260

5361
function isContentTypeJSON(headers) {
62+
if (!headers[CONTENT_TYPE]) {
63+
return false;
64+
}
65+
5466
return _.some(headers[CONTENT_TYPE].split(';'), function (part) {
5567
return part.trim().toLowerCase() === TYPE_JSON;
5668
});
@@ -112,8 +124,7 @@ function doXhr(method, url, headers, data, config, callback) {
112124
var options, timeout;
113125

114126
config = mergeConfig(config);
115-
headers = normalizeHeaders(headers);
116-
headers[CONTENT_TYPE] = headers[CONTENT_TYPE] || TYPE_JSON;
127+
headers = normalizeHeaders(headers, method);
117128
// use config.tmp to store temporary values
118129
config.tmp = config.tmp || {retry_counter: 0};
119130

0 commit comments

Comments
 (0)