Comments (21)
Just got burned on this. Instead of correctly rejecting the promise on invalid (truncated) JSON, it simply returns the orignal string object. Bad bad bad. At least put a warning in the docs about this.
from axios.
@emilyemorehouse is this something you could fix, for the sake of all humanity?
from axios.
I think we should just reject with the parse errors. That'd make axios more predictable and easier to debug.
from axios.
Not sure how others are avoiding this issue after almost 3 years, but I decided to try to work around it for one of my projects. I would like to see how other people have been doing this as well.
I took notes from here and only raise an error for 2xx response codes.
If people find it useful I can make a PR of my commit. If you need this to raise for other response codes, just remove lines 67 and 71 in defaults.js
Another option for those who don’t want to fork is to just override transformResponse, and detect when data is a string, and parse on your own. Unfortunately this means you’ll do more work, and you can’t check the status code if you do that, but at least you have options.
from axios.
I also spent 1 hour today tryng to understand what is happening with some invalid JSON. Just reject the Promise, or at least console.log something...?
from axios.
imagine first finding out in production - not sure where the logical thinking has gone on this one
from axios.
Personally, I think Promise rejection is a good solution. At the very least, one could invoke console.error
in the catch
block to make developers aware of what has happened.
from axios.
I see this has been closed already, but imo neither of the available behaviours from silentJSONParsing are quite perfect for all use cases.
With silentJSONParsing on you can access the status code of the response, but the JSON parsing error is swallowed which isn't ideal.
But with silentJSONParsing off there's no longer any way to see what the status code of the response was, unless I'm missing something.
Effectively it seems like I have to choose between being able to know the status code or the JSON parsing error, but not both.
from axios.
This is among the most disappointing responses to a glaring issue I've seen in an open-source project. This has been an issue for almost 4 years... This is the reason I use my own HTTP package.
from axios.
Fixed in #3688
from axios.
I also think there should be an easy option to retry the request in these cases as it often boils down to a truncated response.
from axios.
+1 here, swallowing errors is never good
from axios.
I just got bitten by this in production today. The current behavior is really unintuitive and wasted a bunch of my time in debugging. Throwing an error would have pointed me in the right direction immediately and would have been helpfully caught/grouped in our error reporting. Even once I found that there was a disconnect between the type of data I got in the response vs. what the header said it should be, it wasn't trivial to find this issue as the explanation.
from axios.
I also encountered a scenario where this behavior is problematic. If the network connection is interrupted partially through the request, axios would not call an error and my application received malformed JSON which obviously led to lots of errors.
I too think that JSON parse errors should be caught and reported. I wasn't keen on forking or modifying the source files so my solution was to transform the response like this:
axios.get(URL, {
transformResponse: [(response) => (JSON.parse(response))],
})
from axios.
My approach to handling this is to wrap all axios requests in a promise, and reject that promise if the response from axios show a response.headers['content-type'] of 'application/json' and a response.data that is a string. From this I infer there was a JSON parsing error.
return new Promise((resolve, reject) => {
axios.request(options)
.then(response => {
if (response.headers && response.headers['content-type'] && response.headers['content-type'].indexOf('application/json') >= 0 && _.isString(response.data)) {
reject(new Error('Error parsing response data as JSON object'));
} else {
resolve(response);
}
});
});
from axios.
Should throw exception
from axios.
@vinnymac Can You please submit PR of Your solution?
from axios.
Just came across this as well, it seems if I manually add: responseType: 'json'
(even though it's the default according to the docs) it at least keeps it form providing the bad data to the result.
from axios.
Is this responseText a problem? like "24\r\n{"success":"true", "payload":"123456"}\r\n0\r\n\r\n", and sometimes it returns "24\r\n{"success":"true", "payload":"123456"}\r\n". Is there anyone who can help, thanks a lot.
from axios.
imagine first finding out in production - not sure where the logical thinking has gone on this one
Generally speaking, that was exactly what we faced....
from axios.
Guys, any updates on this issue?
This is really confusing behavior that I've faced with recently.
from axios.
Related Issues (20)
- New Axios doesn't work with latest LTS Node 20 with --no-experimental-fetch flag HOT 1
- When I send a cross-origin GET request using axios, if the request URL ends with a /, then even if the server is not running, axios will report an error saying 'Access-Control-Allow-Origin' header is present on the requested resource.
- Breaking change in Axios 1.7.4 regarding default host in HTTP adapter
- Breaking change in Axios 0.28.0 regarding default proxy HOT 1
- What's up with all the updates? HOT 6
- Authorization header on Instance vs Request
- Can't put File objects in node.js HOT 1
- Respecting the robots.txt file HOT 3
- CommonJS / ESM mixup when using a lib that has Axios has peer dependency HOT 3
- Why were utils. isFormData (requestData) removed in version 1. x HOT 4
- Transforming async generator functions to the configured target environment is not supported yet
- Builds break when a dependency has Axios installed with a version higher than 1.6.8. HOT 2
- Status of error is undefined whereas devtools shows 502 HOT 5
- Axios not using new header value in interceptor retried request HOT 1
- How can I have access to the Authorization header inside a request interceptor in Axios? HOT 2
- config.paramsSerializer does not work HOT 1
- Axios causes error 503 during sending request to next.js (v 14.2.x) API (app router) HOT 2
- Syntax error on iOS11 or earlier.
- ERR_NETWORK while performing PUT API with mutli-part formdata in android devices - AxiosError: Network Error at handleError
- 🏁 RoSctober 2024: The Open Source Developer Challenge is about to start HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from axios.