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

Commit 9ba24c5

Browse files
committed
fix($http): allow empty json response
When a response contains an `application/json` header and the response is empty the response should be left as is. Fixes #9532 Closes #9562
1 parent 4474633 commit 9ba24c5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/ng/http.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ function $HttpProvider() {
771771
function transformResponse(response) {
772772
// make a copy since the response must be cacheable
773773
var resp = extend({}, response);
774-
if(config.method === 'HEAD'){
774+
if (!response.data) {
775775
resp.data = response.data;
776776
} else {
777777
resp.data = transformData(response.data, response.headers, config.transformResponse);

test/ng/httpSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,16 @@ describe('$http', function() {
11161116
expect(callback.mostRecentCall.args[0]).toEqual('');
11171117
});
11181118

1119+
it('should not attempt to deserialize json for an empty response whose header contains application/json', function(){
1120+
//per http spec for Content-Type, HEAD request should return a Content-Type header
1121+
//set to what the content type would have been if a get was sent
1122+
$httpBackend.expect('GET', '/url').respond('', {'Content-Type': 'application/json'});
1123+
$http({method: 'GET', url: '/url'}).success(callback);
1124+
$httpBackend.flush();
1125+
1126+
expect(callback).toHaveBeenCalledOnce();
1127+
expect(callback.mostRecentCall.args[0]).toEqual('');
1128+
});
11191129

11201130
it('should not deserialize tpl beginning with ng expression', function() {
11211131
$httpBackend.expect('GET', '/url').respond('{{some}}');

0 commit comments

Comments
 (0)