mirror of
https://github.com/tenrok/axios.git
synced 2026-05-30 15:24:11 +03:00
Allow ArrayBuffer and related views as data
In order to push binary data under the form of ArrayBuffer and its related views (Int8Array, ...) one needs not to stringify those. For the XHR adapter there is nothing to do as it natively supports ArrayBuffer in req.send(). Node's http adapter supports only string or Buffer thus a transformation to Buffer is required before setting content length header.
This commit is contained in:
@@ -150,6 +150,7 @@ This is the available config options for making requests. Only the `url` is requ
|
||||
|
||||
// `transformRequest` allows changes to the request data before it is sent to the server
|
||||
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||
// The last function in the array must return a string or an ArrayBuffer
|
||||
transformRequest: [function (data) {
|
||||
// Do whatever you want to transform the data
|
||||
|
||||
@@ -174,6 +175,7 @@ This is the available config options for making requests. Only the `url` is requ
|
||||
|
||||
// `data` is the data to be sent as the request body
|
||||
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||
// When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash
|
||||
data: {
|
||||
firstName: 'Fred'
|
||||
},
|
||||
|
||||
Vendored
+40
-4
@@ -161,10 +161,16 @@ var axios =
|
||||
|
||||
module.exports = {
|
||||
transformRequest: [function (data) {
|
||||
return utils.isObject(data) &&
|
||||
!utils.isFile(data) &&
|
||||
!utils.isBlob(data) ?
|
||||
JSON.stringify(data) : data;
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
return data;
|
||||
}
|
||||
if (utils.isArrayBufferView(data)) {
|
||||
return data.buffer;
|
||||
}
|
||||
if (utils.isObject(data) && !utils.isFile(data) && !utils.isBlob(data)) {
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
return data;
|
||||
}],
|
||||
|
||||
transformResponse: [function (data) {
|
||||
@@ -208,6 +214,30 @@ var axios =
|
||||
return toString.call(val) === '[object Array]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
||||
*/
|
||||
function isArrayBuffer(val) {
|
||||
return toString.call(val) === '[object ArrayBuffer]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a view on an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
||||
*/
|
||||
function isArrayBufferView(val) {
|
||||
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
||||
return ArrayBuffer.isView(val);
|
||||
} else {
|
||||
return (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a String
|
||||
*
|
||||
@@ -349,6 +379,8 @@ var axios =
|
||||
|
||||
module.exports = {
|
||||
isArray: isArray,
|
||||
isArrayBuffer: isArrayBuffer,
|
||||
isArrayBufferView: isArrayBufferView,
|
||||
isString: isString,
|
||||
isNumber: isNumber,
|
||||
isObject: isObject,
|
||||
@@ -483,6 +515,10 @@ var axios =
|
||||
}
|
||||
}
|
||||
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
data = new DataView(data);
|
||||
}
|
||||
|
||||
// Send the request
|
||||
request.send(data);
|
||||
};
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -4,6 +4,7 @@ var transformData = require('./../transformData');
|
||||
var utils = require('./../utils');
|
||||
var http = require('http');
|
||||
var url = require('url');
|
||||
var Buffer = require('buffer').Buffer;
|
||||
|
||||
module.exports = function httpAdapter(resolve, reject, config) {
|
||||
// Transform request data
|
||||
@@ -13,6 +14,10 @@ module.exports = function httpAdapter(resolve, reject, config) {
|
||||
config.transformRequest
|
||||
);
|
||||
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
data = new Buffer(new Uint8Array(data));
|
||||
}
|
||||
|
||||
// Merge headers
|
||||
var headers = utils.merge(
|
||||
defaults.headers.common,
|
||||
|
||||
@@ -87,6 +87,10 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
}
|
||||
}
|
||||
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
data = new DataView(data);
|
||||
}
|
||||
|
||||
// Send the request
|
||||
request.send(data);
|
||||
};
|
||||
+10
-4
@@ -11,10 +11,16 @@ var CONTENT_TYPE_APPLICATION_JSON = {
|
||||
|
||||
module.exports = {
|
||||
transformRequest: [function (data) {
|
||||
return utils.isObject(data) &&
|
||||
!utils.isFile(data) &&
|
||||
!utils.isBlob(data) ?
|
||||
JSON.stringify(data) : data;
|
||||
if (utils.isArrayBuffer(data)) {
|
||||
return data;
|
||||
}
|
||||
if (utils.isArrayBufferView(data)) {
|
||||
return data.buffer;
|
||||
}
|
||||
if (utils.isObject(data) && !utils.isFile(data) && !utils.isBlob(data)) {
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
return data;
|
||||
}],
|
||||
|
||||
transformResponse: [function (data) {
|
||||
|
||||
@@ -12,6 +12,30 @@ function isArray(val) {
|
||||
return toString.call(val) === '[object Array]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
||||
*/
|
||||
function isArrayBuffer(val) {
|
||||
return toString.call(val) === '[object ArrayBuffer]';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a view on an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
||||
*/
|
||||
function isArrayBufferView(val) {
|
||||
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
||||
return ArrayBuffer.isView(val);
|
||||
} else {
|
||||
return (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a String
|
||||
*
|
||||
@@ -153,6 +177,8 @@ function merge(obj1/*, obj2, obj3, ...*/) {
|
||||
|
||||
module.exports = {
|
||||
isArray: isArray,
|
||||
isArrayBuffer: isArrayBuffer,
|
||||
isArrayBufferView: isArrayBufferView,
|
||||
isString: isString,
|
||||
isNumber: isNumber,
|
||||
isObject: isObject,
|
||||
|
||||
@@ -45,6 +45,42 @@ describe('wrapper', function () {
|
||||
}
|
||||
});
|
||||
|
||||
it('should support binary data as array buffer', function () {
|
||||
var input = new Int8Array(2);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: input.buffer
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var output = new Int8Array(request.params.buffer);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
});
|
||||
|
||||
it('should support binary data as array buffer view', function () {
|
||||
var input = new Int8Array(2);
|
||||
input[0] = 1;
|
||||
input[1] = 2;
|
||||
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/foo',
|
||||
data: input
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var output = new Int8Array(request.params.buffer);
|
||||
expect(output.length).toEqual(2);
|
||||
expect(output[0]).toEqual(1);
|
||||
expect(output[1]).toEqual(2);
|
||||
});
|
||||
|
||||
it('should remove content-type if data is empty', function () {
|
||||
axios({
|
||||
method: 'post',
|
||||
|
||||
Reference in New Issue
Block a user