[transport] set the content-type header based on the serialization type

This commit is contained in:
spalger
2015-11-16 17:36:15 -06:00
parent 6cfabff215
commit 391f2286ce
2 changed files with 20 additions and 5 deletions

View File

@ -28,6 +28,8 @@ Json.prototype.serialize = function (val, replacer, spaces) {
} }
}; };
Json.prototype.serialize.contentType = 'application/json';
/** /**
* Parse a JSON string, if it is already parsed it is ignored * Parse a JSON string, if it is already parsed it is ignored
* @param {String} str - the string to parse * @param {String} str - the string to parse
@ -57,3 +59,5 @@ Json.prototype.bulkBody = function (val) {
return body; return body;
}; };
Json.prototype.bulkBody.contentType = 'application/x-ldjson';

View File

@ -136,6 +136,11 @@ Transport.prototype.request = function (params, cb) {
var ret; // the object returned to the user, might be a promise var ret; // the object returned to the user, might be a promise
var defer; // the defer object, will be set when we are using promises. var defer; // the defer object, will be set when we are using promises.
var body = params.body;
var headers = !params.headers ? {} : _.transform(params.headers, function (headers, val, name) {
headers[String(name).toLowerCase()] = val;
});
self.log.debug('starting request', params); self.log.debug('starting request', params);
// determine the response based on the presense of a callback // determine the response based on the presense of a callback
@ -153,14 +158,20 @@ Transport.prototype.request = function (params, cb) {
ret.abort = abortRequest; ret.abort = abortRequest;
} }
if (params.body && params.method === 'GET') { if (body && params.method === 'GET') {
_.nextTick(respond, new TypeError('Body can not be sent with method "GET"')); _.nextTick(respond, new TypeError('Body can not be sent with method "GET"'));
return ret; return ret;
} }
// serialize the body // serialize the body
if (params.body) { if (body) {
params.body = self.serializer[params.bulkBody ? 'bulkBody' : 'serialize'](params.body); var serializer = self.serializer;
var serializeFn = serializer[params.bulkBody ? 'bulkBody' : 'serialize'];
body = serializeFn.call(serializer, body);
if (!headers['content-type']) {
headers['content-type'] = serializeFn.contentType;
}
} }
if (params.hasOwnProperty('maxRetries')) { if (params.hasOwnProperty('maxRetries')) {
@ -175,8 +186,8 @@ Transport.prototype.request = function (params, cb) {
method: params.method, method: params.method,
path: params.path || '/', path: params.path || '/',
query: params.query, query: params.query,
body: params.body, body: body,
headers: params.headers headers: headers
}; };
function sendReqWithConnection(err, _connection) { function sendReqWithConnection(err, _connection) {