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

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

Closed
berniegp opened this issue Jul 12, 2020 · 5 comments
Closed

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

berniegp opened this issue Jul 12, 2020 · 5 comments

Comments

@berniegp
Copy link

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

@cdesch
Copy link

cdesch commented Jul 25, 2020

I am running into a same issue with axios 0.19.0 and XMLHttpRequest 1.8.0.

i have the config with responseType = 'json', but responseData contains a string of the response and data is empty.

Removing the XMLHttpRequest fixed the issue, although I need to figure out what is causing it as it is a dependency for a different package.

@cdesch
Copy link

cdesch commented Jul 25, 2020

This is also in 0.20.0-0

Screen Shot 2020-07-25 at 2 23 24 PM

@berniegp
Copy link
Author

In the meantime, this configuration is a workaround:

axios.create({
  responseType: 'json',
});

@cdesch
Copy link

cdesch commented Jul 26, 2020

Thanks @berniegp, but it persists with that param set. The docs also say that json is the default responseType but I set it anyway when creating the axios instance.

To get the request to work, I have to remove the xmlhttprequest dependency. In my specific case, it is citysdk that depends on xmlhttprequest, so I may have to find a new way adding the functionality as a work around.

Here is my code:

import {store} from '../store';
import {getAPIBaseUrl} from './app_urls';
import axios from "axios";

const defaultOptions = {
  baseURL: getAPIBaseUrl(),
  responseType: "json",
  headers: {
    'Content-Type': 'application/json'
  },
  crossDomain: true
};

// Create instance
const instance = axios.create(defaultOptions);

// Set the AUTH token for any request if authenticated
instance.interceptors.request.use(function (config) {
  const token = store.getState().session.token;
  if (token) {
    config.headers = {...config.headers, ...token}
  }
  return config;
});

export default instance;

@jasonsaayman
Copy link
Member

I think this is an issue that has occurred due to us changing axios to allow the browser to decide the response type, since we have gone back and forth a lot on this we will probably not make any new changes till we cut a v1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants