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
This commit is contained in:
Igor Minar 2012-02-28 12:11:03 -08:00
parent 83155e8fbe
commit 1a5bebd927
2 changed files with 21 additions and 8 deletions

View file

@ -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);

View file

@ -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) {