klippa-app/nativescript-http

Unexpected end of JSON input

eikaramba opened this issue · 5 comments

When i do

let res = await Http.request({
                    url:"xxxxx",
                        method: "DELETE,
                        headers: {
                          Authorization: "Bearer XXXX"
                       }
               });
console.log("res",res);

and the server responds with an empty body (which is valid for DELETE) and status 204. then i get the above error. it seems this library tries to parse the body no matter what.

Which platform(s) does your issue occur on?

  • Android 11

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.

... just gonna leave this here #50

@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?

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.

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.