response.json() gives an error "unexpected end of input" if the data sent back from request is null.
See original GitHub issueThis 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:
- Created 8 years ago
- Reactions:23
- Comments:11 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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?In any case, the power is in your hands. We can’t decide your app’s logic for you.
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.