Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1a5bebd

Browse files
committed
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
1 parent 83155e8 commit 1a5bebd

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/service/http.js

+4
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,10 @@ function $HttpProvider() {
470470
reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn),
471471
promise;
472472

473+
// strip content-type if data is undefined
474+
if (isUndefined(config.data)) {
475+
delete reqHeaders['Content-Type'];
476+
}
473477

474478
// send request
475479
promise = sendReq(config, reqData, reqHeaders);

test/service/httpSpec.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,11 @@ describe('$http', function() {
340340

341341
it('should send custom headers', function() {
342342
$httpBackend.expect('GET', '/url', undefined, function(headers) {
343-
return headers['Custom'] == 'header' && headers['Content-Type'] == 'application/json';
343+
return headers['Custom'] == 'header';
344344
}).respond('');
345345

346346
$http({url: '/url', method: 'GET', headers: {
347347
'Custom': 'header',
348-
'Content-Type': 'application/json'
349348
}});
350349

351350
$httpBackend.flush();
@@ -364,25 +363,25 @@ describe('$http', function() {
364363

365364

366365
it('should set default headers for POST request', function() {
367-
$httpBackend.expect('POST', '/url', undefined, function(headers) {
366+
$httpBackend.expect('POST', '/url', 'messageBody', function(headers) {
368367
return headers['Accept'] == 'application/json, text/plain, */*' &&
369368
headers['X-Requested-With'] == 'XMLHttpRequest' &&
370369
headers['Content-Type'] == 'application/json';
371370
}).respond('');
372371

373-
$http({url: '/url', method: 'POST', headers: {}});
372+
$http({url: '/url', method: 'POST', headers: {}, data: 'messageBody'});
374373
$httpBackend.flush();
375374
});
376375

377376

378377
it('should set default headers for PUT request', function() {
379-
$httpBackend.expect('PUT', '/url', undefined, function(headers) {
378+
$httpBackend.expect('PUT', '/url', 'messageBody', function(headers) {
380379
return headers['Accept'] == 'application/json, text/plain, */*' &&
381380
headers['X-Requested-With'] == 'XMLHttpRequest' &&
382381
headers['Content-Type'] == 'application/json';
383382
}).respond('');
384383

385-
$http({url: '/url', method: 'PUT', headers: {}});
384+
$http({url: '/url', method: 'PUT', headers: {}, data: 'messageBody'});
386385
$httpBackend.flush();
387386
});
388387

@@ -399,20 +398,30 @@ describe('$http', function() {
399398

400399

401400
it('should override default headers with custom', function() {
402-
$httpBackend.expect('POST', '/url', undefined, function(headers) {
401+
$httpBackend.expect('POST', '/url', 'messageBody', function(headers) {
403402
return headers['Accept'] == 'Rewritten' &&
404403
headers['X-Requested-With'] == 'XMLHttpRequest' &&
405404
headers['Content-Type'] == 'Rewritten';
406405
}).respond('');
407406

408-
$http({url: '/url', method: 'POST', headers: {
407+
$http({url: '/url', method: 'POST', data: 'messageBody', headers: {
409408
'Accept': 'Rewritten',
410409
'Content-Type': 'Rewritten'
411410
}});
412411
$httpBackend.flush();
413412
});
414413

415414

415+
it('should not send Content-Type header if request data/body is undefined', function() {
416+
$httpBackend.expect('POST', '/url', undefined, function(headers) {
417+
return !headers.hasOwnProperty('Content-Type');
418+
}).respond('');
419+
420+
$http({url: '/url', method: 'POST'});
421+
$httpBackend.flush();
422+
});
423+
424+
416425
it('should set the XSRF cookie into a XSRF header', inject(function($browser) {
417426
function checkXSRF(secret) {
418427
return function(headers) {

0 commit comments

Comments
 (0)