From 1a5bebd927ecd22f9c34617642fdf58fe3f62efb Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Tue, 28 Feb 2012 12:11:03 -0800 Subject: [PATCH] fix($http): don't send Content-Type header when no data When a http request has no data (body), we should not send the Content-Type header as it causes problems for some server-side frameworks. Closes #749 --- src/service/http.js | 4 ++++ test/service/httpSpec.js | 25 +++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/service/http.js b/src/service/http.js index 5dd87d76b633..17cc68ea0e51 100644 --- a/src/service/http.js +++ b/src/service/http.js @@ -470,6 +470,10 @@ function $HttpProvider() { reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn), promise; + // strip content-type if data is undefined + if (isUndefined(config.data)) { + delete reqHeaders['Content-Type']; + } // send request promise = sendReq(config, reqData, reqHeaders); diff --git a/test/service/httpSpec.js b/test/service/httpSpec.js index 4c8471d8fdf2..43dbf3aff055 100644 --- a/test/service/httpSpec.js +++ b/test/service/httpSpec.js @@ -340,12 +340,11 @@ describe('$http', function() { it('should send custom headers', function() { $httpBackend.expect('GET', '/url', undefined, function(headers) { - return headers['Custom'] == 'header' && headers['Content-Type'] == 'application/json'; + return headers['Custom'] == 'header'; }).respond(''); $http({url: '/url', method: 'GET', headers: { 'Custom': 'header', - 'Content-Type': 'application/json' }}); $httpBackend.flush(); @@ -364,25 +363,25 @@ describe('$http', function() { it('should set default headers for POST request', function() { - $httpBackend.expect('POST', '/url', undefined, function(headers) { + $httpBackend.expect('POST', '/url', 'messageBody', function(headers) { return headers['Accept'] == 'application/json, text/plain, */*' && headers['X-Requested-With'] == 'XMLHttpRequest' && headers['Content-Type'] == 'application/json'; }).respond(''); - $http({url: '/url', method: 'POST', headers: {}}); + $http({url: '/url', method: 'POST', headers: {}, data: 'messageBody'}); $httpBackend.flush(); }); it('should set default headers for PUT request', function() { - $httpBackend.expect('PUT', '/url', undefined, function(headers) { + $httpBackend.expect('PUT', '/url', 'messageBody', function(headers) { return headers['Accept'] == 'application/json, text/plain, */*' && headers['X-Requested-With'] == 'XMLHttpRequest' && headers['Content-Type'] == 'application/json'; }).respond(''); - $http({url: '/url', method: 'PUT', headers: {}}); + $http({url: '/url', method: 'PUT', headers: {}, data: 'messageBody'}); $httpBackend.flush(); }); @@ -399,13 +398,13 @@ describe('$http', function() { it('should override default headers with custom', function() { - $httpBackend.expect('POST', '/url', undefined, function(headers) { + $httpBackend.expect('POST', '/url', 'messageBody', function(headers) { return headers['Accept'] == 'Rewritten' && headers['X-Requested-With'] == 'XMLHttpRequest' && headers['Content-Type'] == 'Rewritten'; }).respond(''); - $http({url: '/url', method: 'POST', headers: { + $http({url: '/url', method: 'POST', data: 'messageBody', headers: { 'Accept': 'Rewritten', 'Content-Type': 'Rewritten' }}); @@ -413,6 +412,16 @@ describe('$http', function() { }); + it('should not send Content-Type header if request data/body is undefined', function() { + $httpBackend.expect('POST', '/url', undefined, function(headers) { + return !headers.hasOwnProperty('Content-Type'); + }).respond(''); + + $http({url: '/url', method: 'POST'}); + $httpBackend.flush(); + }); + + it('should set the XSRF cookie into a XSRF header', inject(function($browser) { function checkXSRF(secret) { return function(headers) {