Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 4025883

Browse files
realitykingpkozlowski-opensource
authored andcommitted
fix($http): don't convert FormData objects to JSON
This won't enable FormData uploads in itself, as the Content-Type is automatically set to application/json. Closes #10373
1 parent c437d0a commit 4025883

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"isWindow": false,
5252
"isScope": false,
5353
"isFile": false,
54+
"isFormData": false,
5455
"isBlob": false,
5556
"isBoolean": false,
5657
"isPromiseLike": false,

src/Angular.js

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
isWindow: true,
4646
isScope: true,
4747
isFile: true,
48+
isFormData: true,
4849
isBlob: true,
4950
isBoolean: true,
5051
isPromiseLike: true,
@@ -566,6 +567,11 @@ function isFile(obj) {
566567
}
567568

568569

570+
function isFormData(obj) {
571+
return toString.call(obj) === '[object FormData]';
572+
}
573+
574+
569575
function isBlob(obj) {
570576
return toString.call(obj) === '[object Blob]';
571577
}

src/ng/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function $HttpProvider() {
142142

143143
// transform outgoing request data
144144
transformRequest: [function(d) {
145-
return isObject(d) && !isFile(d) && !isBlob(d) ? toJson(d) : d;
145+
return isObject(d) && !isFile(d) && !isBlob(d) && !isFormData(d) ? toJson(d) : d;
146146
}],
147147

148148
// default headers

test/ng/httpSpec.js

+9
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,15 @@ describe('$http', function() {
991991
$http({ method: 'POST', url: '/url', data: blob });
992992
});
993993

994+
it('should ignore FormData objects', function() {
995+
if (!window.FormData) return;
996+
997+
var formData = new FormData();
998+
formData.append('angular', 'is great');
999+
1000+
$httpBackend.expect('POST', '/url', '[object FormData]').respond('');
1001+
$http({ method: 'POST', url: '/url', data: formData });
1002+
});
9941003

9951004
it('should have access to request headers', function() {
9961005
$httpBackend.expect('POST', '/url', 'header1').respond(200);

0 commit comments

Comments
 (0)