mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
var utils = require('./utils');
|
|
|
|
var JSON_START = /^\s*(\[|\{[^\{])/;
|
|
var JSON_END = /[\}\]]\s*$/;
|
|
var PROTECTION_PREFIX = /^\)\]\}',?\n/;
|
|
var DEFAULT_CONTENT_TYPE = {
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
};
|
|
|
|
module.exports = {
|
|
transformRequest: [function (data, headers) {
|
|
if (utils.isArrayBuffer(data)) {
|
|
return data;
|
|
}
|
|
if (utils.isArrayBufferView(data)) {
|
|
return data.buffer;
|
|
}
|
|
if (utils.isObject(data) && !utils.isFile(data) && !utils.isBlob(data)) {
|
|
// Set application/json if no Content-Type has been specified
|
|
if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
|
|
headers['Content-Type'] = 'application/json;charset=utf-8';
|
|
}
|
|
return JSON.stringify(data);
|
|
}
|
|
return data;
|
|
}],
|
|
|
|
transformResponse: [function (data) {
|
|
if (typeof data === 'string') {
|
|
data = data.replace(PROTECTION_PREFIX, '');
|
|
if (JSON_START.test(data) && JSON_END.test(data)) {
|
|
data = JSON.parse(data);
|
|
}
|
|
}
|
|
return data;
|
|
}],
|
|
|
|
headers: {
|
|
common: {
|
|
'Accept': 'application/json, text/plain, */*'
|
|
},
|
|
patch: utils.merge(DEFAULT_CONTENT_TYPE),
|
|
post: utils.merge(DEFAULT_CONTENT_TYPE),
|
|
put: utils.merge(DEFAULT_CONTENT_TYPE)
|
|
},
|
|
|
|
xsrfCookieName: 'XSRF-TOKEN',
|
|
xsrfHeaderName: 'X-XSRF-TOKEN'
|
|
}; |