mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-10 15:54:42 +00:00
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:
parent
83155e8fbe
commit
1a5bebd927
2 changed files with 21 additions and 8 deletions
|
|
@ -470,6 +470,10 @@ function $HttpProvider() {
|
||||||
reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn),
|
reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn),
|
||||||
promise;
|
promise;
|
||||||
|
|
||||||
|
// strip content-type if data is undefined
|
||||||
|
if (isUndefined(config.data)) {
|
||||||
|
delete reqHeaders['Content-Type'];
|
||||||
|
}
|
||||||
|
|
||||||
// send request
|
// send request
|
||||||
promise = sendReq(config, reqData, reqHeaders);
|
promise = sendReq(config, reqData, reqHeaders);
|
||||||
|
|
|
||||||
|
|
@ -340,12 +340,11 @@ describe('$http', function() {
|
||||||
|
|
||||||
it('should send custom headers', function() {
|
it('should send custom headers', function() {
|
||||||
$httpBackend.expect('GET', '/url', undefined, function(headers) {
|
$httpBackend.expect('GET', '/url', undefined, function(headers) {
|
||||||
return headers['Custom'] == 'header' && headers['Content-Type'] == 'application/json';
|
return headers['Custom'] == 'header';
|
||||||
}).respond('');
|
}).respond('');
|
||||||
|
|
||||||
$http({url: '/url', method: 'GET', headers: {
|
$http({url: '/url', method: 'GET', headers: {
|
||||||
'Custom': 'header',
|
'Custom': 'header',
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}});
|
}});
|
||||||
|
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
|
@ -364,25 +363,25 @@ describe('$http', function() {
|
||||||
|
|
||||||
|
|
||||||
it('should set default headers for POST request', 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, */*' &&
|
return headers['Accept'] == 'application/json, text/plain, */*' &&
|
||||||
headers['X-Requested-With'] == 'XMLHttpRequest' &&
|
headers['X-Requested-With'] == 'XMLHttpRequest' &&
|
||||||
headers['Content-Type'] == 'application/json';
|
headers['Content-Type'] == 'application/json';
|
||||||
}).respond('');
|
}).respond('');
|
||||||
|
|
||||||
$http({url: '/url', method: 'POST', headers: {}});
|
$http({url: '/url', method: 'POST', headers: {}, data: 'messageBody'});
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should set default headers for PUT request', function() {
|
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, */*' &&
|
return headers['Accept'] == 'application/json, text/plain, */*' &&
|
||||||
headers['X-Requested-With'] == 'XMLHttpRequest' &&
|
headers['X-Requested-With'] == 'XMLHttpRequest' &&
|
||||||
headers['Content-Type'] == 'application/json';
|
headers['Content-Type'] == 'application/json';
|
||||||
}).respond('');
|
}).respond('');
|
||||||
|
|
||||||
$http({url: '/url', method: 'PUT', headers: {}});
|
$http({url: '/url', method: 'PUT', headers: {}, data: 'messageBody'});
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -399,13 +398,13 @@ describe('$http', function() {
|
||||||
|
|
||||||
|
|
||||||
it('should override default headers with custom', 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' &&
|
return headers['Accept'] == 'Rewritten' &&
|
||||||
headers['X-Requested-With'] == 'XMLHttpRequest' &&
|
headers['X-Requested-With'] == 'XMLHttpRequest' &&
|
||||||
headers['Content-Type'] == 'Rewritten';
|
headers['Content-Type'] == 'Rewritten';
|
||||||
}).respond('');
|
}).respond('');
|
||||||
|
|
||||||
$http({url: '/url', method: 'POST', headers: {
|
$http({url: '/url', method: 'POST', data: 'messageBody', headers: {
|
||||||
'Accept': 'Rewritten',
|
'Accept': 'Rewritten',
|
||||||
'Content-Type': '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) {
|
it('should set the XSRF cookie into a XSRF header', inject(function($browser) {
|
||||||
function checkXSRF(secret) {
|
function checkXSRF(secret) {
|
||||||
return function(headers) {
|
return function(headers) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue