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

Use the proper transformers for specific dataType and responseType values. #811

Closed
rubennorte opened this issue Apr 2, 2017 · 11 comments · Fixed by #2424
Closed

Use the proper transformers for specific dataType and responseType values. #811

rubennorte opened this issue Apr 2, 2017 · 11 comments · Fixed by #2424
Projects

Comments

@rubennorte
Copy link
Member

Implement and document proper transformation of request and response body according to dataType and responseType options.

This will fix: #375, #448, #765, #793

@ClementParis016
Copy link

Maybe also #724 and #907

@codeclown
Copy link
Contributor

I think there are two sides to this, right?

  1. Fixing the behaviour when responseType: 'text' should be considered an essential bugfix which addresses a bug in current behaviour.
  2. Implementing dataType would be a new feature, and it also is confusing because currently the automatic type detection of request body is quite nice and works.

The landscape for fixing the responseType bug is quite hazardous:

  • This test asserts that a response containing a valid JSON string (and content-type header) is parsed as JSON automatically, regardless of responseType:

testJSON: function (test) {

  • This test asserts that also in case of failure (non-2xx response), if the response contains a valid JSON string (even without content-type header), it is parsed automatically:

it('should return JSON when rejecting', function (done) {

Both of these stem from the fact that response parsing has defaulted to always trying to parse JSON , regardless of responseType or headers['content-type'].

@ClementParis016
Copy link

Then, if we want to keep the default parsing behavior a possible solution could be to add an option to disable automatic parsing?

@chinesedfan
Copy link
Collaborator

chinesedfan commented Jan 30, 2020

Here are my conclusions,

@Scriptim
Copy link

Scriptim commented Apr 20, 2020

axios/lib/defaults.js

Lines 57 to 65 in 6642ca9

transformResponse: [function transformResponse(data) {
/*eslint no-param-reassign:0*/
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch (e) { /* Ignore */ }
}
return data;
}],

I really don't think that the current default parsing behavior is sensible.

It may even cause the data to be altered under certain circumstances, e. g.:
Let's say a server sends a 16 character string (with according header) of digits: 4201172030730738. The default behavior would convert this to the integer value 4201172030730738, although this is obviously not a JSON object (even if it is a valid JSON value). In case the string is e.g. 9100000000000001 (i.e. > Number.MAX_SAFE_INTEGER), parsing will cause the value to be changed to 9100000000000000 due to inaccuracy of the float data type.

This is not a far-fetched example, but rather a problem that has really occurred and has required quite a bit of debugging. Especially since this is not adequately documented in my opinion.

I suggest that parsing to JSON is only done if the data is really in JSON format, i. e. the Content-Type header is set to application/json and/or the value of dataType/responseType is set accordingly.

@jasonsaayman jasonsaayman linked a pull request Jul 1, 2020 that will close this issue
storycraft added a commit to storycraft/node-kakao that referenced this issue Aug 1, 2020
@jasonsaayman jasonsaayman removed this from To do in v0.20.0 Aug 25, 2020
@scudette
Copy link

scudette commented Oct 9, 2020

Jut hit this bug as well but I did find a workaround which seems to work for me so might save someone else some time:

axios({
        responseType: 'blob',
        method: 'get',
        url: url,
        params: params,
 }).then((response) => response.data.text()};

@jasonsaayman jasonsaayman added this to To do in v1.0.0 via automation May 14, 2021
@lichader
Copy link

lichader commented Jul 2, 2021

I also got this bug and if you are using the alias methods, you can always override the default transformer:

const axiosResponse = await this.httpClient.get(
    `url`,
    {
        transformResponse: (data) => {
            return data;
        },
    }
);

@lucrus73
Copy link

FYI I've just replaced Axois with Got because of this.

@jasonsaayman
Copy link
Member

Default parsing has been changed, please open a new issue should this still be a issue.

@gerarts
Copy link

gerarts commented Nov 1, 2021

Ran into this on 0.24.0. Thanks @lucrus73 , Got works great!

@landsman
Copy link

landsman commented Jun 9, 2022

I had a problem with using axios to file download, where I want to setup responseType: 'arraybuffer'.
In case of error response I was receiving JSON response from the server, but axios was doing conversion to arraybuffer as well in the catch block.

So I have to convert the arraybuffer response to json by this:

const data = await JSON.parse(new TextDecoder().decode(error.response.data));

For people facing the same issue...

andreax79 pushed a commit to andreax79/airflow-code-editor that referenced this issue Aug 12, 2022
- Migrate from Vue 2 to Vue 3
- Refactor History View
- Refactor Workspace View
- Refactor Files and Editor components
- Refactor CSS
- Replace Bootstap Modals with Vue-universal-modal
- Replace Font Awesome with Material Icons

### Fix

- Raise the correct exception at file loading and show the error in the editor
- Fix exception related to axios/axios#811
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v1.0.0
  
To do