Skip to content

Default reponseType is not json #7253

@leozhu7

Description

@leozhu7

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.

axios/lib/defaults/index.js

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

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

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions