question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

response.json() gives an error "unexpected end of input" if the data sent back from request is null.

See original GitHub issue

This is what I have:

this.authTokenStore.get()
    .then(authToken => {
        return fetch(this.serverUrl + '/REST/search?searchTerms=' + text, {
            method: 'get',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'X-Client-Auth-Token': authToken
            }
        })
    })
    .then(this._checkStatus)
    .then(this._parseJSON)
    .then(serverGetSearchSuccess)
    .catch(serverGetSearchError);

What I don’t understand and would need some help, it is with the fact that the response has a status 200, then goes to parseJSON but then goes back to checkStatus and throws that error “unexpected end of input” (debugging step by step using the chrome debugger).

My question I guess is how could I check for the body of the request in the this._parseJSON function and detect there if it is null and do a return Promise.resolve(response) instead of response.json()

I am not sure if that should be posted on some site like SO, if yes I’m sorry for the inconvenience.

Thx for your help

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:23
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

149reactions
mislavcommented, Jan 29, 2016

This is a usage question and as such should be asked on sites like SO instead of here.

Ideally, the server shouldn’t return an invalid (empty) response body to an application/json request, especially because it responded with HTTP 200. But since it misbehaves, you can catch those occurrences inside .catch(serverGetSearchError). I’m not sure why would you want to resolve a promise in that case, like nothing bad happened.

But if you want to avoid a parse error at all costs, why can’t you change your _parseJSON function?

_parseJSON: function(response) {
  return response.text().then(function(text) {
    return text ? JSON.parse(text) : {}
  })
}

In any case, the power is in your hands. We can’t decide your app’s logic for you.

25reactions
abologna-r7commented, Feb 1, 2017

This is a super late response, but anyways my 2 cents on this issue is that if you’re expecting a null response body, with a status code of > 200, you can always send a 204 (no content) response back from the server and handle it without the use of response.json() call.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fetch() unexpected end of input - Stack Overflow
With an opaque response, we won't be able to read the data returned or view the status of the request, meaning we can't...
Read more >
Unexpected end of JSON input Error in JavaScript | bobbyhadz
The "Unexpected end of JSON input" error occurs when trying to parse invalid JSON using the JSON.parse or $.parseJSON methods. Trying to parse...
Read more >
Uncaught SyntaxError: Unexpected end of JSON input
A common error encountered by JavaScript programmers is the Uncaught SyntaxError: Unexpected end of JSON input. This is usually observed when the coder...
Read more >
Need help on fetch - JavaScript - SitePoint Forums
So here I am: I have a node's backend which returns a json object. ... I managed to get to the same unexpected...
Read more >
Troubleshooting in Athena - AWS Documentation
Troubleshoot errors in Athena. ... HIVE_CURSOR_ERROR: Unexpected end of input stream ... NULL or incorrect data errors when trying to read JSON data....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found