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

fix($http): allow empty json response #9562

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ function $HttpProvider() {
function transformResponse(response) {
// make a copy since the response must be cacheable
var resp = extend({}, response);
if(config.method === 'HEAD'){
if (!response.data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we maybe special case 204 and HEAD, and just set the value to undefined instead?

if (!response.data && (config.method === 'HEAD' || response.status === 204)) {
  delete resp.data; // or resp.data = undefined;
}

Because really, an empty JSON string is a programming error in other cases.

Up to you guys though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IgorMinar What do you think?

resp.data = response.data;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be resp.data = undefined.

} else {
resp.data = transformData(response.data, response.headers, config.transformResponse);
Expand Down
10 changes: 10 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,16 @@ describe('$http', function() {
expect(callback.mostRecentCall.args[0]).toEqual('');
});

it('should not attempt to deserialize json for an empty response whose header contains application/json', function(){
//per http spec for Content-Type, HEAD request should return a Content-Type header
//set to what the content type would have been if a get was sent
$httpBackend.expect('GET', '/url').respond('', {'Content-Type': 'application/json'});
$http({method: 'GET', url: '/url'}).success(callback);
$httpBackend.flush();

expect(callback).toHaveBeenCalledOnce();
expect(callback.mostRecentCall.args[0]).toEqual('');
});

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