Axios: Sometime response.data is string instead of object

Created on 13 Aug 2018  ·  43Comments  ·  Source: axios/axios

I don't know why but with the same api, sometime axios's response.data is string instead of object. See the below figure.

image

Context

  • axios version: 0.18.0
  • Environment: React native Expo SDK 29, window 10

Most helpful comment

I found that this happens when the json response from the server is invalid, use a json validator like https://jsonlint.com/ to make sure that your json is correctly formatted

All 43 comments

@huy-lv,

Is the content-type the same for both responses?

@robmcguinness Yes, it's application/json

I see the same behaviour. I found that in my case the API response would sometimes contain invalid JSON data, i.e. JSON.parse would throw. For some reason Axios seems to swallow this error, and ends up setting the invalid JSON string on the data property.

I got this problem, too. When i check response.data of problem case, some invalid JSON String like " data added to response, that make JSON.parse got error and return string.

I've checked with Axios, XMLHttpRequest, fetch
1./ Axios and XMLHttpRequest got same problem
2./ fetch work perfectly

I know there are some chars can lead to that problem, just need to be sure.
Could you capture the response and attach it as file?

@OpenGG sorry for late response, what you need here :D
https://1drv.ms/t/s!AnakDtCf8CG5hIgn2Hemy6zanFi50Q

@huy-lv Not what I'm expecting. Please save the response with curl, then attach the saved file by dropping it to this issue's reply area.

@huy-lv

Never mind, found it.
I pasted it on gist and tried loading with axios, can't see any error.

file: https://gist.github.com/OpenGG/ac72e79f9ef088e993213473116c0cd0
demo: https://codepen.io/OpenGG/pen/bxEqKL

i know, this problem does not happen all the time, it's about 1-2 times each 10 requests (the same API, same response)

@OpenGG it happen on React-Native. Web App work perfectly.

@nighttiger1990

Could you make an online demo on expo, and share it? This demo should make the same request with both Axios and XMLHttpRequest, to determine whether Axios or RN cause this issue.

@OpenGG can you give me your mail i'll send you api, i can't public that
https://github.com/nighttiger1990/axiosBug.git

Any luck with this issue? I'm facing it too

@Druthi now im using 'fetch' instead of 'axios'

any update?

This might not your case but in my case:
My API response string is not valid JSON, so axios cannot parse to JSON, then it return raw string instead.

I have migrated my app (redux-saga,axios) to webpack4 and now response.data is a string instead of object. The migration changes I made are minimal to compile successfully
package.json
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-middleware": "^3.6.1",
"webpack-dev-server": "^3.2.1"
"file-loader": "^3.0.1",

webpack-config.js
added mode: 'development', to module.exports
commented out colors: true, in devServer
commented out json-loader

I ran JSON.parse(response.data) and the JSON is corrupt. (Unexpected token : in JSON at position 8181)

            "municipality": "ddrNote": ""      should be

            "municipality": "",
            "addrNote": ""

I compared with my pre migration webpack3 code where the JSON in response.data is OK (object). So the migrated code is managing to corrupt the JSON in response.data and that is why it is appearing as a string. So now the question is why is the JSON corrupt

I solved the problem by downgrading to "webpack-dev-server": "3.1.10"

I have a devServer proxy which I guess is the cause of the response corruption for versions greater then 3.1.10

@petercutting it is also a problem in production environment, haven't you observed?

@nyilmaz my problem turned out to be caused by webpack-dev-server. webpack-dev-server would not be used in a production environment

@petercutting did you open an issue against webpack-dev-server? I couldn't find on.

@goodmorninggoaway I dont remember. probably not

Irrespective of the webpack-dev-server issues, Axios shouldn't silently swallow JSON.parse exceptions, right?

I was just testing the robustness of an app by sending malformed JSON from the backend. The Axios request silently fails to decode the JSON. The header content-type still says "application/json", but response.data is the raw unparsed string, and my app crashes unexpectedly as a follow up.

It's quite crazy that a library with 60k stars has such a basic bug for half a year :confused:

Irrespective of the webpack-dev-server issues, Axios shouldn't silently swallow JSON.parse exceptions, right?

Parfectly valid

Any update on this bug for react native!?

I found that this happens when the json response from the server is invalid, use a json validator like https://jsonlint.com/ to make sure that your json is correctly formatted

The error swallowing issue is being tracked, I will be closing this issue as it's essentially a duplicate of it.

Please reach out if you have any questions.

@fed135 please link the relevant issue if any, thanks.

Anyone solved this yet?, I'm using laravel as my API, so i logged my response before sending it to my app. The logged response is a valid JSON but the received response has the last "}" brace missing at the end, making it an invalid response. I don't know why this is behaving like this on my mobile app, cause for the frontend app it which uses axios too it works fine, tried on postman too, it works fine. any ideas or solutions pls??

It happens when data gets too large :( I didnt have any problems since the data array was small!

@matinzd I paginate my data, and only return 20

@matinzd and also, my fronend app uses the same endpoint, and it doesn't experience that, only the mobile app with react native does sometimes

@fed135 Is this the right issue to track as a follow up? #61

please my laravel api is coming out as a string alongside