Comments (9)
I'll check for Windows support, shouldn't be too hard.
About the JSON Parse, sending an empty string to JSON.parse should not crash your app, it should just result in an error "JS: ERROR SyntaxError: Unexpected end of JSON input".
Are you sure there wasn't something else that caused this? I think console.log() triggers a toString() on the content and that is causing some issues. I'll try to see if I can reproduce it.
from nativescript-http.
Ok, I found out the crash:
console.log calls toString on the result, the toString method on the result is this:
let str: string;
if (encoding) {
str = decodeResponse(result.raw, encoding);
} else if (result.responseAsString) {
str = result.responseAsString;
}
if (typeof str === "string") {
return str;
} else {
throw new Error("Response content may not be converted to string");
}
Which is the same as in core HTTP. When the result is not a string, it will throw the error, and console.log does not like it. So it's a bug in console.log implementation of NS. I will open a bug in core.
from nativescript-http.
I'll check for Windows support, shouldn't be too hard.
a simple one liner result.context = result.context.replace(/\\/g, '/');
is enough to fix the issue.
About the JSON Parse, sending an empty string to JSON.parse should not crash your app, it should just result in an error "JS: ERROR SyntaxError: Unexpected end of JSON input".
That's exactly the error I got, which triggered a '.catch' on a promise when sending a http request which at that point in time still had some broken code that lead to an app crash.
Are you sure there wasn't something else that caused this? I think console.log() triggers a toString() on the content and that is causing some issues. I'll try to see if I can reproduce it.
Yea, like I thought - it is a bug with 'console.log'. Anyways, a quick try/catch for the win! xD
from nativescript-http.
Is it possible that you were trying to get the result as json? Because a console.log would not trigger that.
However, I did find a bug in console.log in NS, when you have an object that implements toString()
, and you console.log that object, and the toString throws an error, the whole app crashes, so maybe something like that happened to you? I will open a bug report soon.
For the Windows paths you can probably just use path.sep
from nodejs as path separator instead of hardcoded slash. I will add that soon.
from nativescript-http.
Is it possible that you were trying to get the result as json? Because a console.log would not trigger that.
deleteRequest: (resource, params = {}) =>
httpClient(resource + generateParamQuery(params.query, "?"), {
...params,
method: "DELETE",
}).then((response) => {
const result = response;
try { console.log(TAG, "[fetchDelete-Response]", resource, result); } catch {} // NOTE: This line is haunted. For whatever magic reason this line calls .toJSON() when stringifying the output and the JSON input happens to be an empty string.
return result.ok ? result : Promise.reject(result);
}).catch((e) => {
console.log(TAG, "[fetchDelete-Error]", resource + generateParamQuery(params.query, "?"), e);
throw(e);
}
),
// ... after the 'deleteRequest' was sent, the returned 'response' is an object that contains a function 'json: () => Promise.resolve(response.content.toJSON())'
Here's a debugger screenshot without try/catch:
Spooky.
from nativescript-http.
Can you tell me which http client you are using? I don't really think this is something we should solve in this plugin.
from nativescript-http.
httpClient is basically just a 'fetch' wrapper that uses 'nativescript-http' Http.request method.
import { Http } from "@nativescript/core";
export const fetch = (url, options = {}) => { // fetch adapter for 'Http.request'
if (url instanceof Request) {
options = (({ mode, headers, method, _bodyText }) => ({ mode, method, headers: headers.map, body: _bodyText }))(url);
url = url.url;
}
options.url = url;
options.timeout = options.timeout || 3000;
options.content = options.content || options.body;
return Http.request(options).then((response) => {
response.status = response.statusCode;
response.ok = response.statusCode >= 200 && response.statusCode < 300;
response.json = () => Promise.resolve(response.content.toJSON());
return response;
});
};
Anyways, this 'console.log' issue is just something I randomly encountered after installing 'nativescript-http' plugin. It's baffling, sure, but quite easy to handle.
from nativescript-http.
I have just done a test. When you console.log response
, it does not execute the json method, and thus also does not result in the given error.
from nativescript-http.
Fixed with #52
from nativescript-http.
Related Issues (20)
- Update OkHttp 4.10 -> 4.12
- trying to send binary data in formdata it is in console.log but not in payload HOT 1
- 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
- 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
- Unexpected end of JSON input HOT 5
- 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.