mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Fixing response with utf-8 BOM can not parse to json (#2419)
* fix: remove byte order marker (UTF-8 BOM) when transform response * fix: remove BOM only utf-8 * test: utf-8 BOM * fix: incorrect param name Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -235,6 +235,9 @@ module.exports = function httpAdapter(config) {
|
|||||||
var responseData = Buffer.concat(responseBuffer);
|
var responseData = Buffer.concat(responseBuffer);
|
||||||
if (config.responseType !== 'arraybuffer') {
|
if (config.responseType !== 'arraybuffer') {
|
||||||
responseData = responseData.toString(config.responseEncoding);
|
responseData = responseData.toString(config.responseEncoding);
|
||||||
|
if (!config.responseEncoding || config.responseEncoding === 'utf8') {
|
||||||
|
responseData = utils.stripBOM(responseData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
response.data = responseData;
|
response.data = responseData;
|
||||||
|
|||||||
+15
-1
@@ -312,6 +312,19 @@ function extend(a, b, thisArg) {
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
|
||||||
|
*
|
||||||
|
* @param {string} content with BOM
|
||||||
|
* @return {string} content value without BOM
|
||||||
|
*/
|
||||||
|
function stripBOM(content) {
|
||||||
|
if (content.charCodeAt(0) === 0xFEFF) {
|
||||||
|
content = content.slice(1);
|
||||||
|
}
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isArray: isArray,
|
isArray: isArray,
|
||||||
isArrayBuffer: isArrayBuffer,
|
isArrayBuffer: isArrayBuffer,
|
||||||
@@ -333,5 +346,6 @@ module.exports = {
|
|||||||
forEach: forEach,
|
forEach: forEach,
|
||||||
merge: merge,
|
merge: merge,
|
||||||
extend: extend,
|
extend: extend,
|
||||||
trim: trim
|
trim: trim,
|
||||||
|
stripBOM: stripBOM
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -74,6 +74,26 @@ describe('supports http with nodejs', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should allow passing JSON with BOM', function (done) {
|
||||||
|
var data = {
|
||||||
|
firstName: 'Fred',
|
||||||
|
lastName: 'Flintstone',
|
||||||
|
emailAddr: 'fred@example.com'
|
||||||
|
};
|
||||||
|
|
||||||
|
server = http.createServer(function (req, res) {
|
||||||
|
res.setHeader('Content-Type', 'application/json;charset=utf-8');
|
||||||
|
var bomBuffer = Buffer.from([0xEF, 0xBB, 0xBF])
|
||||||
|
var jsonBuffer = Buffer.from(JSON.stringify(data));
|
||||||
|
res.end(Buffer.concat([bomBuffer, jsonBuffer]));
|
||||||
|
}).listen(4444, function () {
|
||||||
|
axios.get('http://localhost:4444/').then(function (res) {
|
||||||
|
assert.deepEqual(res.data, data);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should redirect', function (done) {
|
it('should redirect', function (done) {
|
||||||
var str = 'test response';
|
var str = 'test response';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user