Comments (5)
@manciuszz Your problem was that the toJson() is automatically called when you do console.log
on the response object right? Because that's not something that is caused by nativescript-http
. That's rather nativescript console.log automatically executing the methods. You could try to wrap it in a new Promise
like this:
response.json = () => new Promise((resolve, reject) => {
resolve(response.content.toJSON());
});
Or if you want to do it real proper:
response.json = () => new Promise((resolve, reject) => {
try {
const parsedJSON = response.content.toJSON();
resolve(parsedJSON);
} catch (e) {
reject(e);
}
});
That will probably fix it as that returns a promise that still needs to be resolved, while your previous implementation returns a Promise.resolve
, and executes directly when you execute response.json()
, instead of when you do response.json().then()
.
@eikaramba
I understand the problem. This library is a drop-in replacement for the NativeScript Core HTTP, and thus tries to replicate the behavior. This library behaves the same as core in this case, as can be seen here and here for Android and here and here for iOS.
I rather do not change that behavior by default.
However, I could introduce a "Safe mode" that would do extra checks before executing JSON.parse
, what do you think?
from nativescript-http.
ok so this is not an issue in this library, i called content.toJson() on an empty response. however i still would argue that the toJson method should get some error handling for such cases. for example if the body is empty return either null or an empty object. but do not crash.
from nativescript-http.
... just gonna leave this here #50
from nativescript-http.
wow super interesting because i struggled with the console.log error myself and it was driving me nuts. so thank you for that explanation. i am not going crazy afterall :)
i wasn'T aware that the default nativescript library has the same "problem". so i might aggree with you leaving it like it is and putting the responsibility on the developer to catch this accordingly.
one thing could also be to output a console message and informing that the response was not a valid json so that it is clear why this is happening. again, i could see developers doing a simple console.log(response) to check and then wonder why it is not working at all.
from nativescript-http.
I have created a NativeScript runtime issue about this odd problem: NativeScript/android#1693
The main issue is that NativeScript called this method toJSON, which the console.log
for some reason always calls, if it's not there, but a toString
is there, it will execute toString
.
from nativescript-http.
Related Issues (20)
- Update OkHttp 4.10 -> 4.12
- NativeScript 7 update HOT 12
- Keep uploading images in background on iOS HOT 1
- [FR] run big opps in threads HOT 1
- Cannot find runtime for instance=com.tns.gen.okhttp3.Callback HOT 2
- Does This Support Binary File Upload (Image) in Formdata Submit HOT 16
- Callback upload or download available? HOT 1
- Why not suggest a custom webpack configuration? HOT 2
- is there support to select/send a client certificate(for authentication PKCS12) during the TLS handshake? HOT 6
- Public key pinning...backup keys HOT 2
- [Question] Webpack 5 Error HOT 8
- NS8 Webpack path replacement module does not take into account Windows 10 backslashes HOT 9
- ReferenceError: Cannot access 'Cache' before initialization HOT 1
- [Review Request] Roadmap Step - Cache Control HOT 2
- Access to native error / request error type to differentiate network errors HOT 2
- how to read cookies HOT 1
- Support for HTTP/3? HOT 1
- Feature request: Certificate transparency for Android HOT 1
- Request hangs when using a Self-Signed certificate HOT 11
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 nativescript-http.