mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
Improved FormData support; (#4448)
* Fixed isFormData predicate;
Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment through `form-data` package;
* Added the `form-data` package as a dependency for the server build;
Added tests for FormData payload;
* Added FormData automatic serialization section;
Refactored cancellation section;
* Reworked toFormData helper;
Expose toFormData helper as a static method;
Refactored transform request;
Added kindOf, kindOfTest, endsWith, isTypedArray util;
Refactored utils.js to use kindOf for tests;
* Fixed isFormData predicate; (#4413)
Added support for automatic object serialization to FormData if `Content-Type` is `multipart/form-data`;
Added support for FormData to be overloaded using `config.env.FormData` option;
Added support for FormData in node.js environment using `form-data` package;
(cherry picked from commit 73e3bdb883)
* Added shortcut methods `postForm`, `putForm`, `patchForm` to submit a Form;
Added ability to submit FileList object as a FormData;
Updated README.md;
* Updated README.md;
This commit is contained in:
@@ -9,6 +9,8 @@ var fs = require('fs');
|
||||
var path = require('path');
|
||||
var pkg = require('./../../../package.json');
|
||||
var server, proxy;
|
||||
var FormData = require('form-data');
|
||||
var formidable = require('formidable');
|
||||
|
||||
describe('supports http with nodejs', function () {
|
||||
|
||||
@@ -494,7 +496,7 @@ describe('supports http with nodejs', function () {
|
||||
|
||||
it('should display error while parsing params', function (done) {
|
||||
server = http.createServer(function () {
|
||||
|
||||
|
||||
}).listen(4444, function () {
|
||||
axios.get('http://localhost:4444/', {
|
||||
params: {
|
||||
@@ -1078,5 +1080,46 @@ describe('supports http with nodejs', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow passing FormData', function (done) {
|
||||
var form = new FormData();
|
||||
var file1= Buffer.from('foo', 'utf8');
|
||||
|
||||
form.append('foo', "bar");
|
||||
form.append('file1', file1, {
|
||||
filename: 'bar.jpg',
|
||||
filepath: 'temp/bar.jpg',
|
||||
contentType: 'image/jpeg'
|
||||
});
|
||||
|
||||
server = http.createServer(function (req, res) {
|
||||
var receivedForm = new formidable.IncomingForm();
|
||||
|
||||
receivedForm.parse(req, function (err, fields, files) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
|
||||
res.end(JSON.stringify({
|
||||
fields: fields,
|
||||
files: files
|
||||
}));
|
||||
});
|
||||
}).listen(4444, function () {
|
||||
axios.post('http://localhost:4444/', form, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data'
|
||||
}
|
||||
}).then(function (res) {
|
||||
assert.deepStrictEqual(res.data.fields,{foo: 'bar'});
|
||||
|
||||
assert.strictEqual(res.data.files.file1.mimetype,'image/jpeg');
|
||||
assert.strictEqual(res.data.files.file1.originalFilename,'temp/bar.jpg');
|
||||
assert.strictEqual(res.data.files.file1.size,3);
|
||||
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
var assert = require('assert');
|
||||
var isFormData = require('../../../lib/utils').isFormData;
|
||||
var FormData = require('form-data');
|
||||
|
||||
describe('utils::isFormData', function () {
|
||||
it('should detect the FormData instance provided by the `form-data` package', function () {
|
||||
[1, 'str', {}, new RegExp()].forEach(function (thing) {
|
||||
assert.equal(isFormData(thing), false);
|
||||
});
|
||||
assert.equal(isFormData(new FormData()), true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user