Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding body serializer config method #3727

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ These are the available config options for making requests. Only the `url` is re
return Qs.stringify(params, {arrayFormat: 'brackets'})
},

// `bodySerializer` is an optional function in charge of serializing `body`
bodySerializer: function (params) {
var formData = new FormData();
Object.entries(data).forEach(([key, value]) => {
formData.append(key, value);
});
return formData;
},

// `data` is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'
// When no `transformRequest` is set, must be of one of the following types:
Expand Down
16 changes: 12 additions & 4 deletions examples/upload/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ <h1>file upload</h1>
(function () {
var output = document.getElementById('output');
document.getElementById('upload').onclick = function () {
var data = new FormData();
data.append('foo', 'bar');
data.append('file', document.getElementById('file').files[0]);
var data = {
foo: 'bar',
file: document.getElementById('file').files[0],
};

var config = {
onUploadProgress: function(progressEvent) {
var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
}
},
serializedBody: function(data) {
var formData = new FormData();
Object.entries(data).forEach(([key, value]) => {
formData.append(key, value);
});
return formData;
},
};

axios.put('/upload/server', data, config)
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface AxiosRequestConfig {
headers?: any;
params?: any;
paramsSerializer?: (params: any) => string;
bodySerializer?: (body: any) => any;
data?: any;
timeout?: number;
timeoutErrorMessage?: string;
Expand Down
4 changes: 4 additions & 0 deletions lib/core/Axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Axios.prototype.request = function request(config) {
config.method = 'get';
}

if (config.bodySerializer) {
config.data = config.bodySerializer(config.data);
}

var transitional = config.transitional;

if (transitional !== undefined) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/mergeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function mergeConfig(config1, config2) {
var valueFromConfig2Keys = ['url', 'method', 'data'];
var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
var defaultToConfig2Keys = [
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer', 'bodySerializer',
'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress',
'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent',
Expand Down