[transport] set the content-type header based on the serialization type
This commit is contained in:
@ -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';
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user