Skip to content

Default 'json' responseType is not applied to requests #3116

@berniegp

Description

@berniegp

Describe the bug

The documentation states:

  // `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

However, the responseType of XMLHttpRequest is left unset (same as "text") by default instead of being set set to 'json'. This causes an empty response body to become "" instead of null as expected from the XMLHttpRequest specification :

A JSON response is the return value of these steps:

  1. If response’s body is null, then return null.
  2. Let jsonObject be the result of running parse JSON from bytes on received bytes. If that threw an exception, then return null.
  3. Set response object to jsonObject and return it.

A text response is the return value of these steps:

  1. If response’s body is null, then return the empty string.

To Reproduce

Create a request with the XHR adapter where the server responds with an empty body. Then response.data === "".

Expected behavior

response.data === null (through a default request.responseType = 'json').

Environment

  • Axios Version: 0.19.2
  • Adapter: XHR
  • Browser: Chrome
  • Browser Version: 83
  • OS: Windows 10

Additional context/Screenshots

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