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

responseType: 'text' return Json Object #907

Closed
Airkro opened this issue May 20, 2017 · 29 comments · Fixed by #2424
Closed

responseType: 'text' return Json Object #907

Airkro opened this issue May 20, 2017 · 29 comments · Fixed by #2424

Comments

@Airkro
Copy link

Airkro commented May 20, 2017

// test.txt  -->  adsasdsa55
axios.get('./test.txt', { responseType: 'json' }).then(response => {
    console.log( response.data );
    // output `null`, illegal, awesome!
});

// test.json  -->  [1233]
axios.get('./test.json', { responseType: 'json' }).then(response => {
    console.log( response.data );
    // output Array(1233), ok!
});

But:

// test.txt  -->  [false,5]
axios.get('./test.txt', { responseType: 'text' }).then(response => {
    console.log( typeof response.data );
    // output `object` , not `string`, why?
    console.log( response.data );
    // output Array(false, 5), not "[false,5]"
});
  • axios version: v0.16.1
@pschlump
Copy link

This makes it impossible to get data from google.com and facebook because they prefix responces with while(1); or for(;;); and you need the "text" version of the data so that you can pre-process it.

@drew-r
Copy link

drew-r commented Jul 21, 2017

It seems still uses JSON.parse or something.
I am getting typeof response.data === 'number' for a response of '1702'

@Airkro
Copy link
Author

Airkro commented Jul 24, 2017

@drew-r

because:

responseType indicates the type of data that the server will respond with options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'

@rubennorte
Copy link
Member

This is because JSON.parse is always tried in the response, even if responseType is text. We should fix that indeed.

@rubennorte rubennorte added the bug label Aug 13, 2017
@XuluWarrior
Copy link

I've been bothered by this behaviour as well. In my case none of the data I am requesting is JSON and the caught JSON.parse exception was preventing me leaving "Pause on caught exception" on in my debugger.
Although not a complete fix (it doesn't respect responseType), I was wondering whether the default transformResponse function could respect the "content-type" in the response headers and only attempt to parse as JSON if the content type is application/json. Currently transformResponse doesn't include headers in its signature but it is passed them by the calling transformData function.
e.g.

transformResponse: [function transformResponse(data, headers) {
    /*eslint no-param-reassign:0*/
    if (typeof data === 'string' && headers["content-type"] === "application/json") {
      data = data.replace(PROTECTION_PREFIX, '');
      try {
        data = JSON.parse(data);
      } catch (e) { /* Ignore */ }
    }
    return data;
  }]

@kestereverts
Copy link

Is there any progess on this issue? I was just caught out by this bug. I expected that responseType: 'text' would stop automatic JSON parsing.

@sonicoder86
Copy link

Is it possible to override the default transformResponse method?

@punksta
Copy link

punksta commented Mar 18, 2018

This is critical in case of validating json scheme before parsing or stream parsing.

@punksta
Copy link

punksta commented Mar 18, 2018

can be solved by passing

transformResponse: undefined

to axios or request configs

@ozgur-yalcin
Copy link

can be solved by using arraybuffer as responseType

axios.get('./test.txt', { responseType: 'arraybuffer' }).then(response => {
    var buffer = new Buffer(response.data, 'binary');
    var textdata = buffer.toString(); // for string
    console.log(textdata);
    var jsondata = buffer.toJSON(); // for json
    console.log(jsondata);
});

@TwiN
Copy link

TwiN commented May 13, 2018

@OzqurYalcin that returns [object ArrayBuffer] instead of the original [object Object], which isn't really much help.

In my case, I needed the responseType to be text so that the indentation could be preserved when fetching raw code (e.g. https://raw.githubusercontent.com/TwinProduction/show-my-ip/master/manifest.json), but it didn't work when the real response type was JSON due to Axios' automatic JSON parsing.

However, @punksta's solution of setting transformResponse to undefined fixed the issue.

@sky0014
Copy link

sky0014 commented Aug 17, 2018

Hope fix this issue soon.

@hojas
Copy link

hojas commented Sep 18, 2018

+1

@oatmeaI
Copy link

oatmeaI commented Oct 30, 2018

Is anyone working on this? I'd be happy to take a stab at it.

@YJBeetle
Copy link

YJBeetle commented Dec 7, 2018

response.request.response is text

@zozoh
Copy link

zozoh commented Jan 17, 2019

@kvillaniholland I use transformResponse : undefined to escape the auto json converting as @punksta commented above. And I am waiting this issue can be close also ^_^

@dolsup
Copy link

dolsup commented Jan 24, 2019

I'm suffering under the same problem too.. It doesn't make sense.
@rubennorte 🔔
Is there any progress for it?

@hurui79
Copy link

hurui79 commented Apr 12, 2019

这都2019年了,这个bug居然还在。目前是 通过 重写 transformResponse 方法解决的。希望作者能够重视起来,加个判断config.responseType的类型,然后再决定是否用JSON.parse 去转换数据应该不麻烦啊

@zozoh
Copy link

zozoh commented Apr 12, 2019

@390029659 我已经弃坑了,直接 100+ 行用 XmlHttpRequest 封了一个,完全用不着 axios 了,你要的话,我代码发你,很通用

@hurui79
Copy link

hurui79 commented Apr 12, 2019

@zozoh 好啊,学习瞻仰一下,提前谢谢了

@zozoh
Copy link

zozoh commented Apr 14, 2019

@390029659 已经发你邮箱里了 hrbeta@foxmail.com

@geesmart
Copy link

i use 'transformResponse: undefined' , but fail

this can be solved
transformResponse: [(data) => { return data; }],

version 0.19.0

@crimx
Copy link

crimx commented Aug 4, 2019

It's been over 2 years. What is blocking the issue?

@DoronBrayer-RedisLabs
Copy link

I have the same issue. I get [object Object]. Please help.

@chinesedfan
Copy link
Collaborator

chinesedfan commented Dec 21, 2019

The problem is the default config.transformResponse will always try to parse string to json. You can set another transform function. And please keep an eye on #811.

@Draphar
Copy link

Draphar commented Jan 1, 2020

This issue makes me almost speechless. What did the developers think when writing this? "So the user might not want JSON, but we'll try anyway and then override the user's choice" When is this kind of behaviour desired? A library that patronizes me like this is terrible.

@WoLfulus
Copy link

WoLfulus commented Jan 2, 2020

This issue makes me almost speechless. What did the developers think when writing this? "So the user might not want JSON, but we'll try anyway and then override the user's choice" When is this kind of behaviour desired? A library that patronizes me like this is terrible.

I'm sorry, but what makes me speechless is comments like this. Just don't, keep it to yourself.

Shaance added a commit to Shaance/news-aggregator-api that referenced this issue Apr 19, 2020
@yiyione
Copy link

yiyione commented Apr 27, 2020

Nobody fix this bug?

@RobinDeBaets
Copy link

This bug still happens. Quite annoying when you fetch a piece of JSON data that you want to handle as a raw string.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.