Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected end of JSON input #63

Open
eikaramba opened this issue Dec 16, 2021 · 5 comments
Open

Unexpected end of JSON input #63

eikaramba opened this issue Dec 16, 2021 · 5 comments

Comments

@eikaramba
Copy link

eikaramba commented Dec 16, 2021

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
@eikaramba
Copy link
Author

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.

@manciuszz
Copy link

... just gonna leave this here #50

@jerbob92
Copy link
Contributor

jerbob92 commented Dec 17, 2021

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

@eikaramba
Copy link
Author

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.

@jerbob92
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants