Skip to content

Commit

Permalink
fix($http): don't send Content-Type header when no data
Browse files Browse the repository at this point in the history
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 angular#749
  • Loading branch information
IgorMinar committed Mar 20, 2012
1 parent d361264 commit da9c745
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/service/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
25 changes: 17 additions & 8 deletions test/service/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
});

Expand All @@ -399,20 +398,30 @@ 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'
}});
$httpBackend.flush();
});


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) {
Expand Down

0 comments on commit da9c745

Please sign in to comment.