-
-
Notifications
You must be signed in to change notification settings - Fork 11.5k
Description
Describe the bug
In the document, responseType default value is json
// `responseType` indicates the type of data that the server will respond with
// options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
// browser only: 'blob'
responseType: 'json', // default
But it is undefined when running axios.
If we do not set it manually, it will prevent throwing a JSON parse error, even if silentJSONParsing is set to false when parsing an unexpected JSON string.
Lines 103 to 120 in 8092aee
| const JSONRequested = this.responseType === 'json'; | |
| if (utils.isResponse(data) || utils.isReadableStream(data)) { | |
| return data; | |
| } | |
| if (data && utils.isString(data) && ((forcedJSONParsing && !this.responseType) || JSONRequested)) { | |
| const silentJSONParsing = transitional && transitional.silentJSONParsing; | |
| const strictJSONParsing = !silentJSONParsing && JSONRequested; | |
| try { | |
| return JSON.parse(data, this.parseReviver); | |
| } catch (e) { | |
| if (strictJSONParsing) { | |
| if (e.name === 'SyntaxError') { | |
| throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, this.response); | |
| } | |
| throw e; |
It has a similar issue, but does not change it.
since we have gone back and forth a lot on this we will probably not make any new changes till we cut a v1.
Maybe we should fix it in v1.
#3116
I suggest adding a default value in the
Lines 36 to 40 in 8092aee
| const defaults = { | |
| transitional: transitionalDefaults, | |
| adapter: ['xhr', 'http', 'fetch'], |
To Reproduce
Set the backend controller to return invalid JSON
Or
Use Fiddler to intercept the response and modify the JSON to invalid JSON.
Code snippet
const response = await axios.get(uri, {
transitional: { silentJSONParsing: false },
});Expected behavior
Just set silentJSONParsing as false, when receiving invalid JSON, then throw a JSON parse SyntaxError.
Axios Version
1.13.2
Adapter Version
No response
Browser
No response
Browser Version
No response
Node.js Version
No response
OS
No response
Additional Library Versions
Additional context/Screenshots
