Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): attempt to JSON.parse errors for JSON responses #19773

Closed
wants to merge 1 commit into from

Conversation

alxhub
Copy link
Member

@alxhub alxhub commented Oct 18, 2017

No description provided.

@mary-poppins
Copy link

You can preview 1679728 at https://pr19773-1679728.ngbuilds.io/.

@mary-poppins
Copy link

You can preview fbc0297 at https://pr19773-fbc0297.ngbuilds.io/.

@IgorMinar IgorMinar added area: common/http action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Oct 18, 2017
Prior behavior for HttpClient was to parse the body as JSON when
responseType was set to 'json', even if the response was
unsuccessful. This changed due to a recent bugfix, and
unsuccessful responses had their bodies treated as strings.

There is no guarantee that if a service returns JSON in the
successful case that it will do so for errors. However, users
indicate that most APIs in the wild do work this way. Therefore,
this commit changes the error case behavior to attempt a JSON
parsing of the response body, and falls back on returning it as
a string if that fails.
@mary-poppins
Copy link

You can preview 2a539e6 at https://pr19773-2a539e6.ngbuilds.io/.

@tbosch tbosch closed this in 04ab9f1 Oct 18, 2017
tbosch pushed a commit that referenced this pull request Oct 18, 2017
Prior behavior for HttpClient was to parse the body as JSON when
responseType was set to 'json', even if the response was
unsuccessful. This changed due to a recent bugfix, and
unsuccessful responses had their bodies treated as strings.

There is no guarantee that if a service returns JSON in the
successful case that it will do so for errors. However, users
indicate that most APIs in the wild do work this way. Therefore,
this commit changes the error case behavior to attempt a JSON
parsing of the response body, and falls back on returning it as
a string if that fails.

PR Close #19773
@Linskeyd
Copy link
Contributor

@alxhub,

Should this be present for the error portion as well?

https://github.com/angular/angular/pull/19773/files#diff-26683cfb34fa97883213282055aef1e7R185

@IgorMinar
Copy link
Contributor

IgorMinar commented Oct 20, 2017 via email

@duykhuongkid
Copy link

I have tested the issue. It fixed the issue when the response has content. However, when I return Ok() (Asp.net Core 2.0 - Status code 200). The issue still happen.
I found that because of JSON.Parse() can't parse the empty string. In this case, the body is string empty. So that I think we should check the Body is empty before try to parse.
Please correct me if I wrong

image

@erbsenkoenig
Copy link

erbsenkoenig commented Oct 23, 2017 via email

@Linskeyd
Copy link
Contributor

@IgorMinar,

I'm talking about the XSSI prefix being only stripped for OK statuses.

Take a look here (at a commit prior to the modifications to any of this code path: 452a7ae)

You'll notice that for all statuses (except for 204) the prefix is attempted to be stripped. With the changes on this pull request that have been merged, only the OK status has the XSSI prefix stripped.

Happy to submit a PR for this if you agree with the above.

@IgorMinar
Copy link
Contributor

IgorMinar commented Oct 23, 2017 via email

@alxhub
Copy link
Member Author

alxhub commented Oct 23, 2017

@Linskeyd @IgorMinar

I believe you're correct, it should be stripped.

@Linskeyd
Copy link
Contributor

@alxhub,

Posted PR #19917 when you get a chance.

} else if (!ok && req.responseType === 'json' && typeof body === 'string') {
try {
// Attempt to parse the body as JSON.
body = JSON.parse(body);
Copy link

Choose a reason for hiding this comment

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

Unfortunately this does not support XSSI, there should be similar approach as for OK flow with body.replace(XSSI_PREFIX, '')
see also https://angular.io/guide/security#xssi

@IgorMinar
Copy link
Contributor

IgorMinar commented Oct 31, 2017 via email

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: common/http cla: yes target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants