From fbb125a3af164e52af2f8119175b04cbbed2f331 Mon Sep 17 00:00:00 2001 From: Bruno Baia Date: Tue, 19 Nov 2013 00:30:36 +0100 Subject: [PATCH] fix($http): allow sending Blob data using $http Closes #5012 --- src/.jshintrc | 1 + src/Angular.js | 6 ++++++ src/ng/http.js | 2 +- test/ng/httpSpec.js | 10 ++++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/.jshintrc b/src/.jshintrc index f32caa451ed6..35bba32e73ed 100644 --- a/src/.jshintrc +++ b/src/.jshintrc @@ -64,6 +64,7 @@ "isWindow": false, "isScope": false, "isFile": false, + "isBlob": false, "isBoolean": false, "trim": false, "isElement": false, diff --git a/src/Angular.js b/src/Angular.js index 68ae929539ce..96df13f4ff8d 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -45,6 +45,7 @@ -isWindow, -isScope, -isFile, + -isBlob, -isBoolean, -trim, -isElement, @@ -566,6 +567,11 @@ function isFile(obj) { } +function isBlob(obj) { + return toString.call(obj) === '[object Blob]'; +} + + function isBoolean(value) { return typeof value === 'boolean'; } diff --git a/src/ng/http.js b/src/ng/http.js index 0c54f5bb636e..c7dc64e2c4fd 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -103,7 +103,7 @@ function $HttpProvider() { // transform outgoing request data transformRequest: [function(d) { - return isObject(d) && !isFile(d) ? toJson(d) : d; + return isObject(d) && !isFile(d) && !isBlob(d) ? toJson(d) : d; }], // default headers diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index a7b244831f6f..86ab72ea8bd8 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -989,6 +989,16 @@ describe('$http', function() { }); + it('should ignore Blob objects', function () { + if (!window.Blob) return; + + var blob = new Blob(['blob!'], { type: 'text/plain' }); + + $httpBackend.expect('POST', '/url', '[object Blob]').respond(''); + $http({ method: 'POST', url: '/url', data: blob }); + }); + + it('should have access to request headers', function() { $httpBackend.expect('POST', '/url', 'header1').respond(200); $http.post('/url', 'req', {