Don't process response body for http status 204 (no content) #7301
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The web implementation of the http plugin tries to parse all successful responses, including responses with status 204 - no content. If the
responseType
is'json'
and the response status is 204 with no body, parsing the response withresponse.json()
results in the exception "SyntaxError: Unexpected end of JSON input".This is unexpected, the absence of a response body should not cause an exception if the status code is 204, as the 204 indicates that no body is present.
The ios and Android implementations of the http plugin behave differently in such a case, the Android implementation sets data to an empty string, the ios implementation to "The data couldn't be read because it isn't in the correct format.". They actually never throw an exception, on Android data is set to the input string, on ios to the aforementioned error string.
This pull requests adjusts the web implementation to the Android implementation: if parsing fails, data is set to the input string instead of throwing an exception. On a 204 response with no body, data is set to an empty string.
Same issue in an earlier capacitor version: #6628